I have the following code, which returns a lot of data in the response, so much that I get a NullPointerException when it's being loaded in the android Activity when I scroll down too fast (since not all the data has been initialized yet), no problems if I wait a second and then scroll.
I want a way to delay the subscribe part, so that the Response<GetFeedTopicsResponseBody> is entirely populated with data (none is not initialized) when I call setAdapter. I tried checking response.isSuccessful but that does not work because no problem with the response itself, just the data takes time to deserialize into Java objects from JSON. I also tried onComplete in subscribe but that does not work either.
So I want either a way in RxJava2 to have a boolean value switch to notify the following subscription once it is complete, it will subscribe.
mGetFeedTopicsDisposable = ApiClient.getInstance()
.doGetFeedTopicsQuery(request)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
.subscribe((Response<GetFeedTopicsResponseBody> response) -> {
if (response.body() != null) {
List<Topic> topics = response.body().getTopics();
if (topics != null) {
mBinding.fragmentTopicListRecyclerTopics.setAdapter(TopicListAdapter.getInstance(topics));
if (response.body().isPaginated()) {
mRequestBuilder.setCursor(response.body().getCursor());
}
}
}
}, (Throwable ex) -> {
Log.e(TAG, ex.getMessage());
});
The error message I specifically got was:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.models.User.getThumbnailImageUrl()' on a null object reference
where this User object is set as a field of the Topic object which is added into the list of topics retrieved with getTopics(). If I don't scroll, I don't get this NullPointerException and the thumbnail urls for the Users are loaded properly.

delayoperator, did you try it? reactivex.io/documentation/operators/delay.html - y.allamdelayoperator. You tried it already and it didn't work but how exactly did you try? - lelloman