How to recover properly recover from an interupted uncached request using RoboSpice

前端 未结 1 985
盖世英雄少女心
盖世英雄少女心 2021-01-14 04:37

I have a simple fragment that handles logins for my application. Since I\'m dealing with login requests, I do not want to cache them. This strategy works fine until I introd

相关标签:
1条回答
  • 2021-01-14 05:03

    Use cache. Execute request with some cache key

    spiceManager.execute(request, "your_cache_key", DurationInMillis.ALWAYS_EXPIRED, new AccessTokenResponseRequestListener());
    

    and in listener remove response on this request from cache if it returned successfully before you switched to another activity as you do not want to cache the account information as per your requirement.

    @Override
    public void onRequestFailure(SpiceException e) {
        ....
        spiceManager.removeDataFromCache(AccessTokenResponse.class);
        ....
    }
    
    @Override
    public void onRequestSuccess(AccessTokenResponse accessToken) {
        if (accessToken == null) {
            return;
        }
        ....
        spiceManager.removeDataFromCache(AccessTokenResponse.class);
        ....
    }
    

    In onStart try to get cached response if you switched to another activity and now come back to the previous activity. This return response which arrive after you call spiceManager.shouldStop(). Otherwise return null.

    spiceManager.getFromCache(AccessTokenResponse.class, "your_cache_key", DurationInMillis.ALWAYS_RETURNED, new AccessTokenResponseRequestListener());
    
    0 讨论(0)
提交回复
热议问题