未调用 LiveData observe()

LiveData observe() not called

提问人:Greggz 提问时间:8/29/2018 更新时间:1/19/2022 访问量:10871

问:

我的观察者回调没有被调用,你能指出我做错了什么吗?我的应用程序非常简单,因为我只是在测试这个架构

MainActivity 代码

viewModel = ViewModelProviders.of(this).get(UpcomingMoviesViewModel.class);
viewModel.getUpcomingMovies().observe(this, upcomingMovies -> {
        // NOT CALLED !!!
        for(UpcomingMovies movie : upcomingMovies) {
            Log.d("Upcoming", "Movie - " + movie.movie.title);
        }
    });

View模型代码

private LiveData<List<UpcomingMovies>> mUpcomingMovies;
private MovieRepository movieRepository;

public UpcomingMoviesViewModel(@NonNull Application application ) {
    super(application);
    movieRepository = MovieRepository.getsInstance(application); 
}

public LiveData<List<UpcomingMovies>> getUpcomingMovies() {
    mUpcomingMovies = movieRepository.getUpcomingMovies();
    return mUpcomingMovies;
}

还有我的存储库方法

public LiveData<List<UpcomingMovies>> getUpcomingMovies(){
    if(movieDao.loadUpcomingMovies().getValue() == null) {
        webService.getUpcomingMovies(api_key).
                enqueue(new Callback<MovieResults>() {

                    @Override
                    public void onResponse(Call<MovieResults> call, Response<MovieResults> response) {
                        MovieResults results = response.body();
                        if(results == null) return;
                        new insertAsyncTask(movieDao).execute(results.movies.toArray(new Movie[results.movies.size()]));
                    }

                    @Override
                    public void onFailure(Call<MovieResults> call, Throwable t) {
                        Log.d("Retrofit error", t.getMessage());
                    }
                });
    }
    return movieDao.loadUpcomingMovies();
}

private static class insertAsyncTask extends AsyncTask<Movie, Void, Void> {

    private final MovieDao mAsyncTask;

    insertAsyncTask(MovieDao dao){
        mAsyncTask = dao;
    }

    @Override
    protected Void doInBackground(Movie... movies) {
        long [] results = mAsyncTask.insertMovies(Arrays.asList(movies));
        long [] results2 = mAsyncTask.insertUpcomingMovies(Arrays.asList(movies));
        return null;
    }
}

两者都返回 20 个数字,我将其归因于我收到 20 部电影的事实。 我在这里做错了什么?感谢您抽出宝贵时间接受采访resultsresults2

DAO Android-Room Android-LiveData

评论


答:

2赞 Prashanth Verma 8/29/2018 #1

将 LifeCyclebserver 实现到 viewModel 并将其添加到您的活动中:

getLifecycle().addObserver(viewModel);

评论

1赞 Greggz 8/29/2018
postValue是来自 not 的方法MutableLiveDataLiveData
0赞 Prashanth Verma 8/29/2018
是的,将您的 mUpcomingMovies 初始化为 MutableLiveData。我忘了添加该代码。您还确定数据将到达 viewModel?
0赞 Greggz 8/29/2018
抛出异常,说为 nullmUpcomingMovies
0赞 Prashanth Verma 8/29/2018
像这样初始化:private MutableLiveData<List<UpcomingMovies>> mUpcomingMovies = new MutableLiveData<List<UpcomingMovies>>()
0赞 Greggz 8/29/2018
我正在使用这条线.它仍然没有被调用mUpcomingMovies.postValue(movieRepository.getUpcomingMovies().getValue());
0赞 oyamo 10/27/2020 #2

请记住,您的 viewModel 需要有一个不接受参数的构造函数

评论

2赞 S.L. Barth is on codidact.com 10/27/2020
不确定这是否回答了问题。OP 没有获得 NullReferenceException,因此他们的 ViewModel 似乎已正确实例化。(我猜 OP 从中派生了他们的 ViewModel)。AndroidViewModel
0赞 DPRK 1/19/2022 #3

对于源和目标片段中的 Java,调用;

viewModel = new ViewModelProvider(getActivity()).get(YourViewModel.class);

viewModel = new ViewModelProvider(this).get(YourViewModel.class);

getActivity而不是允许您引用相同的活动this