Problem:
The observer on LiveData<PagedList>
is not triggered after the ItemKeyedDataSource.LoadedInitialCallback.onResult()
is called.
At the time callback.onResult(...)
, the value passed in is correct.
However, nothing happens after that.
Expected: After callback.onResult(...)
, the observer should be able to detect there is a change to LiveData.
Relevant Code: Method for fetching data from network (It's in a class called CommentManager), here we used Firestore:
...
public void loadInitialComments(int pageSize,
ItemKeyedDataSource.LoadInitialCallback<Comment> callback) {
collectionReference.orderBy(DatabaseConstants.FIELD_TIMESTAMP)
.limit(pageSize)
.get()
.addOnSuccessListener(queryDocumentSnapshots -> {
callback.onResult(getComments(queryDocumentSnapshots));
});
}
...
How the function is being called in DataSource class
...
@Override
public void loadInitial(LoadInitialParams<String> params,
LoadInitialCallback<Comment> callback) {
commentManager.loadInitialComments(params.requestedLoadSize, callback);
}
...
ViewModel class
public final class CommentViewModel extends ViewModel {
private final LiveData<PagedList<Comment>> comments;
public CommentViewModel() {
comments = new LivePagedListBuilder<>(new CommentDataSourceFactory(new CommentManager()), config).build();
}
public LiveData<PagedList<Comment>> getComments() {
return comments;
}
How ViewModel is called in main activity
...
CommentViewModel commentViewModel = new ViewModelProvider(this,
new CommentViewModelFactory())
.get(CommentViewModel.class);
commentViewModel.getComments().observe(this, comments -> commentListAdapter.submitList(comments));
...