3

I would like to ask assistance for my error. I used robospice-retrofit for my api and I want to get the cache. In my other sample program the I could get the cache and the value but when i used it in the fragment, I always have a null value of my cache. Btw, I created another class that would handle all my request, is there a problem with this?

Please check my codes: This is created from separate class.

public void roboretroGetTempString(final DomainResponseCallback callback,SpiceManager spiceManager){
    boolean isHasCache = false;
    TestStringRequest graphRequest = new TestStringRequest();
    try {
        if(spiceManager.isDataInCache(String.class,"graph",DurationInMillis.ONE_MINUTE).get()){
            Log.e(TAG,"onRefresh:Has Cache"+spiceManager.getDataFromCache(String.class,"graph"));
        }else{
            /*Execute if cache has expired*/
            Log.e(TAG,"onRefresh:No Cache");
            spiceManager.execute(graphRequest, "graph", DurationInMillis.ONE_MINUTE, new RequestListener<String>() {
                @Override
                public void onRequestFailure(SpiceException spiceException) {
                    spiceException.printStackTrace();
                    callback.onApiResponse("Error", null);
                }
                @Override
                public void onRequestSuccess(String str) {
                    Log.e(TAG,"onRequestSuccess:result-"+str);
              }
            });
        }

    } catch (CacheCreationException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    } catch (CacheLoadingException e) {
        e.printStackTrace();
    }
}

This is the code in my Fragment: DomainResponseCallback is my callback interface. I also passed the SpiceManager as a parameter that I used in the code above.

private void roboLoadTest(){
    GraphController graphController = new GraphController();
    graphController.roboretroGetTempString(new DomainResponseCallback() {
        @Override
        public void onApiResponse(String result, Object obj) {
            progressBar.setVisibility(View.GONE);
            Log.e(TAG,"onApiResponse-"+result);
        }

        @Override
        public void onApiError(String error) {
           Log.e(TAG,"onApiResponse-"+error);
        }
    },getSpiceManager());
}

This is How I set up my Fragment based on the Sample Code and extends my Fragment to this.

 public class RoboFragment extends Fragment{

    private String TAG = RoboFragment.class.getSimpleName();
    /***
     * With {@link com.octo.android.robospice.UncachedSpiceService} there is no cache management. Remember to declare it in
     * AndroidManifest.xml
     */
    private SpiceManager spiceManager = new SpiceManager(RoboSpiceService.class);

    @Override
    public void onStart() {
        super.onStart();
        spiceManager.start(getActivity());
    }

    @Override
    public void onStop() {
        // Please review https://github.com/octo-online/robospice/issues/96 for the reason of that
        // ugly if statement.
        if (spiceManager.isStarted()) {
            spiceManager.shouldStop();
        }
        super.onStop();
    }

    protected SpiceManager getSpiceManager() {
        return spiceManager;
    }

    }

This is my TestRequest`

public class TestStringRequest extends RetrofitSpiceRequest<String,RetrofitApi> {

    public TestStringRequest() {
        super(String.class, RetrofitApi.class);

    }

    @Override
    public String loadDataFromNetwork() throws Exception {
        return getService().getTestRetrofit();
    }

}

My RetrofitApi

@GET(api_reciever+"mytestretrofit")
public String getTestRetrofit();

I don't have any idea on whats missing on my code.

Looking forward for your assistance.

user4144348
  • 313
  • 3
  • 8

1 Answers1

2

A new spice manager is created/started every time a new fragment is open and that spice manager also will close. That is why you will have an empty/null cache. Check this [post].1

In my scenario, I just created a singleton extends with application and created that spicemanager in my main activity(the parent of the fragments). And It works. I don't know if this is the best solution, still searching for more better approach.

public class AppSingleton extends Application{
     private static AppSingleton ourInstance = new AppSingleton();
     private SpiceManager spiceManager= new SpiceManager(RoboSpiceService.class);

     public static AppSingleton getInstance() {
       return ourInstance;
     }

     public SpiceManager getSpiceManager() {
       return spiceManager;
     }
}
chkm8
  • 1,302
  • 1
  • 13
  • 34