碎片中的spiceManager.isDataInCache为空/ null

3
我想请您帮忙解决我的错误。我在我的api中使用了robospice-retrofit,并且想要获取缓存。在我其他的示例程序中,我可以获取缓存和值,但是当我在片段中使用它时,我的缓存总是为空值。顺便说一下,我创建了另一个类来处理所有请求,这有问题吗?
请检查我的代码:这是从单独的类创建的。
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();
    }
}

以下是我 Fragment 中的代码: DomainResponseCallback 是我的回调接口。我还将 SpiceManager 作为参数传递,以便在上述代码中使用。

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());
}

这是我根据示例代码设置我的Fragment并将其扩展到此的方式。

 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();
    }

}

我的 RetrofitApi

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

我对我的代码缺少什么没有任何想法。

期待您的帮助。

1个回答

2
每次打开新的片段时,都会创建/启动一个新的香料管理器,并且该香料管理器也将关闭。这就是为什么您将拥有一个空/ null缓存的原因。请查看此[帖子]。1 在我的情况下,我只是创建了一个扩展应用程序的单例并在我的主活动(片段的父级)中创建了那个spicemanager。它有效地工作。我不知道这是否是最佳解决方案,仍在寻找更好的方法。
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;
     }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接