Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deliverLatestCache() example #51

Closed
devilmac opened this issue Nov 21, 2015 · 2 comments
Closed

deliverLatestCache() example #51

devilmac opened this issue Nov 21, 2015 · 2 comments

Comments

@devilmac
Copy link

Hi konmik,
I'm using this library into my app and I find it great and easy to use. I have a little problem with deliverLatestCache() method. You said that:

deliverLatestCache() is the same as deliverLatest() but in addition it will keep the latest result in memory and will re-deliver it when another instance of a view becomes available (i.e. on configuration change). If you don't want to organize save/restore of a request result in your view (in case if a result is big or it can not be easily saved into Bundle) this method will allow you to make user experience better.

and I'm trying to get this latest result but I don't understand what I have to do with it. Can you make an example of use for this method?

Thanks a lot.

@inorichi
Copy link
Contributor

In your subscription you should call a method of your view that updates the data. When you rotate the screen, the last result will be given again to the view, without repeating the query or network call or whatever. You don't have to do anything else.

You can use restartable*, a pseudocode example here: #42 (comment)

And a real example (with lambdas):

MyPresenter:

private static final int GET_MY_DATA = 1;

@Override
protected void onCreate(Bundle savedState) {
    super.onCreate(savedState);

    restartableLatestCache(GET_MY_DATA,
            () -> Observable.just(1)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread()),
            (view, data) -> {
                view.doSomething(data);
            }, (view, error) -> {
                view.doSomethingOnError(error);
            });

    start(GET_MY_DATA);
}

Or you can use the deilver* methods, for example:

MyPresenter:

@Override
protected void onCreate(Bundle savedState) {
    super.onCreate(savedState);

    Observable.just(1)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .compose(deliverLatestCache())
            .subscribe(
                    split((view, data) -> {
                        view.doSomething(data);
                    }),
                    split((view, error) -> {
                        view.doSomethingOnError(error);
                    }));
}

I would use the restartable approach. It allows you to start the observable whenever you want, with the start(x) method, so that you can provide some parameters to the query first. Just make sure you are not restarting the observable when a configuration change occurs in order to take advantage of these deliver* methods.

If you use the second approach, don't forget to wrap the observable with the add() method, so that it unsubscribes when the presenter is destroyed.

@konmik
Copy link
Owner

konmik commented Dec 10, 2015

@devilmac , did @inorichi anwer your question? You can close the issue if so.

@konmik konmik closed this as completed Dec 11, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants