SpiceManager.isDataInCache is empty / null in fragment

I would like to ask for help for my error. I have used robospice-retrofit for my api and I want to get the cache. In my other example program, I could get the cache and value, but when I used it in a snippet, I always have my cache value null. Btw, I created another class that would handle my entire request, is there any problem with that?

Please check my codes: This is generated from a 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 snippet: DomainResponseCallback is my callback interface. I also passed the SpiceManager as a parameter that I used in the above code.

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 snippet based on the example code and extended my snippet.

 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 have no idea what is missing in my code.

Looking forward to your help.

+3


source to share


1 answer


A new spice manager is created / started every time a new slice is opened and that spice manager is also closed. This is why you will have an empty / null cache. Check out this post. 1

In my scenario, I just created a singleton expandable with an application and created this spicemanager in my main activity (parent snippet). And it works. I don't know if this is the best solution, still looking for a more efficient 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;
     }
}

      

+2


source







All Articles