Search code examples
androidcachinglistenerrobospice

Robospice - In what order should we use execute(), addListenerIfPending(), getFromCache()


There are three possible states of request when we enter an activity.

  1. Not yet started
  2. In process, listener is detached
  3. Request complete

From the documentation, I understand if we use execute() in onStart(), it takes care of cases 1,3 but not case 2(when request is in process). We need to use addListenerIfpending() for that

Workaround I used is use addListenerIfPending() in onStart(), and use execute in onRequestNotFound()

protected  void onStart(){
    super.onStart();
    getSpiceManager().addListenerIfPending(my.class,"mykey",new myRequestListener());
}


    public final class myRequestListener implements PendingRequestListener<result> {

    @Override
    public void onRequestFailure(SpiceException spiceException){

    }

    @Override
    public void onRequestSuccess(final RoundInfo roundInfo) {

    }

    @Override
    public void onRequestNotFound(){

        getSpiceManager().execute(request,"mykey", DurationInMillis.ONE_DAY,new myRequestListener());
    }
}

I want to know if this is the correct way, the way it's meant to be done.

Also, please comment if there will be any performance issues


Solution

  • No, your first statement is wrong.

    execute() will take in charge all 3 cases.

    addListenerIfPending will not trigger any request by itself, it only allows to plug a listener to an already pending request if such a request exists. So case 2.