Created
April 2, 2017 13:41
-
-
Save ErikHellman/c76bff4bc8daf8cb9931d98bf892e151 to your computer and use it in GitHub Desktop.
RxJava & DiffUtil
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void subscribeToData() { | |
Flowable<List<Data>> dataLists = Flowable | |
.interval(0, 3, TimeUnit.SECONDS, Schedulers.computation()) | |
.map(val -> { | |
Log.d(TAG, "Shuffling original list..."); | |
return Data.shuffle(originalList).subList(0, (int) (0.8 * originalList.size())); | |
}) | |
.share(); | |
Flowable<List<Data>> startWith = dataLists | |
.startWith(new ArrayList<Data>(originalList)); | |
this.disposable = Flowable | |
.zip(startWith, dataLists, (current, next) -> { | |
Log.d(TAG, "Calculate diff result..."); | |
MyCallback callback = new MyCallback(current, next); | |
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(callback, true); | |
return Pair.create(diffResult, next); | |
}) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(diffResultListPair -> { | |
Log.d(TAG, "Update adapter!"); | |
adapter.setDataList(diffResultListPair.second); | |
diffResultListPair.first.dispatchUpdatesTo(adapter); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment