Reputation: 697
As I am learning rx java, I have found another problem that I am very curious about. I am trying to create load more with list and rx java android extension using Retrofit. So I have created stream to my List and calling onNext whenever I need more data, this means (Its crucial) 2-3 at the very beggining, I am loading data at batches 10 per request, at first I do have to load them 3 times, one after another, then on every value I do apply a scan method to increase my value
.scan(0, new Func2<Integer, Integer, Integer>() {
@Override
public Integer call(Integer integer, Integer integer2) {
return integer + 1;
}
}
Then doing request using retrofit that returns list of Strings and changing adapter data of course I am calling
.subscribeOn(Schedulers.newThread().observeOn(AndroidSchedulers.mainThread()
But I am getting an exception
java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes.
It seems that adapter data is trying to be modified from background thread, however I have checked, whenever I call adapter method to modify data I am on the main thread. This problem does not occur when I am loading single batch, only when trying to load few and well I cant figure out why its happening. Next side effect is that sometimes second batch is beign loaded before first. I have been messing around with blocking obvservable but without any result.
Thanks for any tips.
Pseudo code on my logic:
CreateListObservable
.scan(increase value by 1)
.flatMap(download batch of values)
.scan(add new batch to existing one)
.subscribe(Schedulers.newThread)
.observeOn(AndroidSchedulers.mainThread)
.subscribe(data -> adapter.changeData(data)
It seems than when I add some sleep between changing data (1s) this error dissappears, weird
Upvotes: 0
Views: 667
Reputation: 2684
Have you tried copying the output array before passing it to the adapter? If it is the array that scan retains, further requests may be editing the content of the array on the background thread due to the scan.
.subscribe(data -> adapter.changeData(data.copy())
Upvotes: 1