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

How to find if a restartable is started already? #63

Closed
sreejithraman opened this issue Jan 10, 2016 · 4 comments
Closed

How to find if a restartable is started already? #63

sreejithraman opened this issue Jan 10, 2016 · 4 comments

Comments

@sreejithraman
Copy link

Is there a method that I can call to see if a certain restartable is started? I have a restartable that is paging, and normally I would just start it in the onCreate() of the presenter but since I need arguments passed to the activity, I have to call a method in the onCreate() of the activity. Now when I rotate the device, the activity is recreated and goes through the whole onCreate() and restarts the restartable. So, I put a boolean check to see if restartable has already started like so:

public void fetchItems(String parentItemId){
        if(!hasStartedPaging) {
            rxPager = new RxPager();
            registerFetchItemsFromServerRestartable(parentItemId); // I just register a restartableReplay 
            start(RESTARTABLE_ID);
            hasStartedPaging = true;
        }
}

Is that how you would recommend doing this or can I replace the hasStartedPaging variable with something that lets me check if a restartable is already started? Or am I completely doing this wrong?

@inorichi
Copy link
Contributor

You can start it when the bundle is null in your activity:

if (savedInstanceState == null) {
    getPresenter().fetchItems(id);
}

If you still want to know if a restartable is running, you will have to copy this class to your project https://github.com/konmik/nucleus/blob/master/nucleus/src/main/java/nucleus/presenter/RxPresenter.java and add a method like this:

public boolean isSubscribed(int restartableId) {
    Subscription s = restartableSubscriptions.get(restartableId);
    return s != null && !s.isUnsubscribed();
}

Edit: Also, don't register the restartable (registerFetchItemsFromServerRestartable(parentItemId);) there. You should register them only in the onCreate method of the presenter. They will do nothing until you call start(x).

@konmik
Copy link
Owner

konmik commented Jan 11, 2016

It looks like a good idea to include isSubscribed into the next version. Thanks, @inorichi !

@konmik konmik closed this as completed Jan 11, 2016
@sreejithraman
Copy link
Author

@inorichi Correct me if I am wrong, but I can't do

if (savedInstanceState == null) {
    getPresenter().fetchItems(id);
}

because after a crash the savedInstanceState will not be null and the presenter also would be recreated, right?

@inorichi
Copy link
Contributor

After a process restart, the restartable will restart itself (as it's name says), that's why you should store that parentItemId in the presenter's bundle so that you don't get a NPE when accessing it.

The wiki recommends you to use Icepick for saving/restoring instances.

Then you declare a variable with saved state in your presenter like so: @State String parentItemId. In your fetchItems method you add this.parentItemId = parentItemId and everything will work fine.

This answer can help you understanding how restartables work too.

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