Search code examples
androidcachingrobospice

RoboSpice: loading from cache does not work after app is resumed


As long as I am staying in the same fragment the cache works perfectly fine. BUT as soon as I am switching the app to the background and then reloading the data onResume(), the cache is not valid anymore and a network connection is made.

Here is my code:

protected void loadData(final OnDataLoadListener onDataLoadListener) {
       final ReverseStringRequestListener listener = new ReverseStringRequestListener(onDataLoadListener);
       final String cacheKey = "cacheKey234234";
       final ConfigFeedRequest request = new ConfigFeedRequest("http://app.tty-eli.com/index.php?id=3");
       getSpiceManager().getFromCacheAndLoadFromNetworkIfExpired(request, cacheKey, DurationInMillis.ONE_SECOND * 50, listener);
}

Any ideas why this might happen? I am creating the SpiceManager in the class header of the fragment like that:

private SpiceManager spiceManager = new SpiceManager(InMemorySpiceService.class);

Here is the log:

10-07 16:07:06.595    5618-5618/at.tty_eli.app D//SpiceManager.java:212﹕ 16:07:06.606 main SpiceManager started.
10-07 16:07:06.605    5618-5618/at.tty_eli.app V/AppointmentFragment_﹕ onResume
10-07 16:07:06.605    5618-5618/at.tty_eli.app D//SpiceManager.java:489﹕ 16:07:06.619 main adding request to request queue
10-07 16:07:06.605    5618-5618/at.tty_eli.app D/AbsListView﹕ onVisibilityChanged() is called, visibility : 0
10-07 16:07:06.605    5618-5618/at.tty_eli.app D/AbsListView﹕ unregisterIRListener() is called
10-07 16:07:06.615    5618-6034/at.tty_eli.app V//SpiceManager.java:1191﹕ 16:07:06.623 SpiceManagerThread 2 Binding to service.
10-07 16:07:06.625    5618-5618/at.tty_eli.app D//SpiceService.java:134﹕ 16:07:06.631 main SpiceService instance created.
10-07 16:07:06.625    5618-5618/at.tty_eli.app D/AbsListView﹕ unregisterIRListener() is called
10-07 16:07:06.625    5618-6034/at.tty_eli.app V//SpiceManager.java:1197﹕ 16:07:06.633 SpiceManagerThread 2 Binding to service succeeded.
10-07 16:07:06.625    5618-6034/at.tty_eli.app D//SpiceManager.java:1245﹕ 16:07:06.637 SpiceManagerThread 2 Waiting for service to be bound.
10-07 16:07:06.645    5618-5618/at.tty_eli.app V//SpiceService.java:506﹕ 16:07:06.656 main Pending requests : 0
10-07 16:07:06.645    5618-5618/at.tty_eli.app V//SpiceService.java:508﹕ 16:07:06.658 main Stop foreground
10-07 16:07:06.655    5618-5618/at.tty_eli.app D//SpiceServiceListenerNotifier.java:33﹕ 16:07:06.662 main Message Queue starting
10-07 16:07:06.655    5618-5618/at.tty_eli.app D//SpiceManager.java:1088﹕ 16:07:06.664 main Bound to service : InMemorySpiceService
10-07 16:07:06.665    5618-6034/at.tty_eli.app D//SpiceManager.java:1252﹕ 16:07:06.673 SpiceManagerThread 2 Bound ok.
10-07 16:07:06.665    5618-6034/at.tty_eli.app D//SpiceManager.java:286﹕ 16:07:06.678 SpiceManagerThread 2 Sending request to service : CachedSpiceRequest
10-07 16:07:06.675    5618-6034/at.tty_eli.app D//RequestProcessor.java:63﹕ 16:07:06.683 SpiceManagerThread 2 Adding request to queue 1121951496: CachedSpiceRequest [requestCacheKey=cacheKey234234, cacheDuration=50000, spiceRequest=at.tty_eli.app.network.ConfigFeedRequest@42c24c50] size is 0
10-07 16:07:06.675    5618-6034/at.tty_eli.app D//RequestProcessor.java:85﹕ 16:07:06.689 SpiceManagerThread 2 Adding entry for type class at.tty_eli.app.network.ConfigFeedResponse and cacheKey cacheKey234234.
10-07 16:07:06.685    5618-6034/at.tty_eli.app D//RequestProgressManager.java:61﹕ 16:07:06.692 SpiceManagerThread 2 Request was added to queue.
10-07 16:07:06.685    5618-6034/at.tty_eli.app D//SpiceServiceListenerNotifier.java:146﹕ 16:07:06.695 SpiceManagerThread 2 Message queue is Handler (android.os.Handler) {42dfa580}
10-07 16:07:06.685    5618-5618/at.tty_eli.app D//SpiceServiceListenerNotifier.java:175﹕ 16:07:06.698 main Processing request added: CachedSpiceRequest [requestCacheKey=cacheKey234234, cacheDuration=50000, spiceRequest=at.tty_eli.app.network.ConfigFeedRequest@42c24c50]
10-07 16:07:06.695    5618-6034/at.tty_eli.app D//RequestProgressManager.java:82﹕ 16:07:06.703 SpiceManagerThread 2 Sending progress PENDING
10-07 16:07:06.695    5618-6034/at.tty_eli.app D//SpiceServiceListenerNotifier.java:146﹕ 16:07:06.708 SpiceManagerThread 2 Message queue is Handler (android.os.Handler) {42dfa580}
10-07 16:07:06.705    5618-5618/at.tty_eli.app V//DefaultRequestListenerNotifier.java:131﹕ 16:07:06.710 main Notifying 1 listeners of progress com.octo.android.robospice.request.listener.RequestProgress@42c24c88
10-07 16:07:06.705    5618-6038/at.tty_eli.app D//DefaultRequestRunner.java:83﹕ 16:07:06.714 Thread-6031 Processing request : CachedSpiceRequest [requestCacheKey=cacheKey234234, cacheDuration=50000, spiceRequest=at.tty_eli.app.network.ConfigFeedRequest@42c24c50]
10-07 16:07:06.705    5618-6034/at.tty_eli.app V//SpiceService.java:506﹕ 16:07:06.713 SpiceManagerThread 2 Pending requests : 1
10-07 16:07:06.705    5618-6038/at.tty_eli.app D//DefaultRequestRunner.java:97﹕ 16:07:06.718 Thread-6031 Loading request from cache : CachedSpiceRequest [requestCacheKey=cacheKey234234, cacheDuration=50000, spiceRequest=at.tty_eli.app.network.ConfigFeedRequest@42c24c50]
10-07 16:07:06.715    5618-6038/at.tty_eli.app D//RequestProgressManager.java:82﹕ 16:07:06.720 Thread-6031 Sending progress READING_FROM_CACHE
10-07 16:07:06.715    5618-6038/at.tty_eli.app D//SpiceServiceListenerNotifier.java:146﹕ 16:07:06.727 Thread-6031 Message queue is Handler (android.os.Handler) {42dfa580}
10-07 16:07:06.715    5618-5618/at.tty_eli.app V//DefaultRequestListenerNotifier.java:131﹕ 16:07:06.729 main Notifying 1 listeners of progress com.octo.android.robospice.request.listener.RequestProgress@42b0d130
10-07 16:07:06.725    5618-6034/at.tty_eli.app V//SpiceService.java:508﹕ 16:07:06.716 SpiceManagerThread 2 Stop foreground
10-07 16:07:06.725    5618-6038/at.tty_eli.app D//LruCacheObjectPersister.java:47﹕ 16:07:06.732 Thread-6031 Miss from lru cache for cacheKey234234
10-07 16:07:06.725    5618-6038/at.tty_eli.app D//LruCacheObjectPersister.java:47﹕ 16:07:06.737 Thread-6031 Miss from lru cache for cacheKey234234
10-07 16:07:06.735    5618-6038/at.tty_eli.app D//DefaultRequestRunner.java:129﹕ 16:07:06.741 Thread-6031 Cache content not available or expired or disabled
10-07 16:07:06.745    5618-6038/at.tty_eli.app D//DefaultRequestRunner.java:148﹕ 16:07:06.751 Thread-6031 Calling netwok request.
10-07 16:07:06.745    5618-6038/at.tty_eli.app D//RequestProgressManager.java:82﹕ 16:07:06.755 Thread-6031 Sending progress LOADING_FROM_NETWORK
10-07 16:07:06.755    5618-6038/at.tty_eli.app D//SpiceServiceListenerNotifier.java:146﹕ 16:07:06.760 Thread-6031 Message queue is Handler (android.os.Handler) {42dfa580}
10-07 16:07:06.755    5618-5618/at.tty_eli.app V//DefaultRequestListenerNotifier.java:131﹕ 16:07:06.763 main Notifying 1 listeners of progress com.octo.android.robospice.request.listener.RequestProgress@42c11c38
10-07 16:07:10.435    5618-6038/at.tty_eli.app D//DefaultRequestRunner.java:151﹕ 16:07:10.442 Thread-6031 Network request call ended.
10-07 16:07:10.435    5618-6038/at.tty_eli.app D//DefaultRequestRunner.java:171﹕ 16:07:10.447 Thread-6031 Start caching content...
10-07 16:07:10.445    5618-6038/at.tty_eli.app D//RequestProgressManager.java:82﹕ 16:07:10.453 Thread-6031 Sending progress WRITING_TO_CACHE
10-07 16:07:10.455    5618-6038/at.tty_eli.app D//SpiceServiceListenerNotifier.java:146﹕ 16:07:10.459 Thread-6031 Message queue is Handler (android.os.Handler) {42dfa580}
10-07 16:07:10.455    5618-5618/at.tty_eli.app V//DefaultRequestListenerNotifier.java:131﹕ 16:07:10.462 main Notifying 1 listeners of progress com.octo.android.robospice.request.listener.RequestProgress@42a3aa28
10-07 16:07:10.455    5618-6038/at.tty_eli.app D//LruCacheObjectPersister.java:74﹕ 16:07:10.467 Thread-6031 Put in lru cache for cacheKey234234
10-07 16:07:10.465    5618-6038/at.tty_eli.app D//RequestProgressManager.java:82﹕ 16:07:10.473 Thread-6031 Sending progress COMPLETE
10-07 16:07:10.475    5618-6038/at.tty_eli.app D//SpiceServiceListenerNotifier.java:146﹕ 16:07:10.480 Thread-6031 Message queue is Handler (android.os.Handler) {42dfa580}
10-07 16:07:10.475    5618-5618/at.tty_eli.app V//DefaultRequestListenerNotifier.java:131﹕ 16:07:10.483 main Notifying 1 listeners of progress com.octo.android.robospice.request.listener.RequestProgress@42a977a0
10-07 16:07:10.475    5618-6038/at.tty_eli.app D//SpiceServiceListenerNotifier.java:146﹕ 16:07:10.488 Thread-6031 Message queue is Handler (android.os.Handler) {42dfa580}
10-07 16:07:10.485    5618-5618/at.tty_eli.app V//DefaultRequestListenerNotifier.java:166﹕ 16:07:10.491 main Notifying 1 listeners of request success
10-07 16:07:10.485    5618-5618/at.tty_eli.app V//DefaultRequestListenerNotifier.java:172﹕ 16:07:10.494 main Notifying ReverseStringRequestListener
10-07 16:07:10.485    5618-5618/at.tty_eli.app V/AppointmentFragment_﹕ onDataReady
10-07 16:07:10.495    5618-6038/at.tty_eli.app V//RequestProgressManager.java:161﹕ 16:07:10.503 Thread-6031 Removing CachedSpiceRequest [requestCacheKey=cacheKey234234, cacheDuration=50000, spiceRequest=at.tty_eli.app.network.ConfigFeedRequest@42c24c50]  size is 1
10-07 16:07:10.505    5618-6038/at.tty_eli.app D//RequestProgressManager.java:91﹕ 16:07:10.508 Thread-6031 Sending all request complete.
10-07 16:07:10.515    5618-6038/at.tty_eli.app V//SpiceService.java:495﹕ 16:07:10.520 Thread-6031 Pending requests : 0
10-07 16:07:10.515    5618-6038/at.tty_eli.app D//SpiceServiceListenerNotifier.java:146﹕ 16:07:10.524 Thread-6031 Message queue is Handler (android.os.Handler) {42dfa580}
10-07 16:07:10.545    5618-5618/at.tty_eli.app D/AbsListView﹕ unregisterIRListener() is called
10-07 16:07:10.565    5618-6038/at.tty_eli.app D//DefaultRequestRunner.java:295﹕ 16:07:10.569 Thread-6031 It tooks 3828 ms to process request CachedSpiceRequest [requestCacheKey=cacheKey234234, cacheDuration=50000, spiceRequest=at.tty_eli.app.network.ConfigFeedRequest@42c24c50].

tty


Solution

  • The reason why this is happening is that as soon as there is no activity bound to the service anymore, the service stops. In order to avoid loosing all cached data, write it to the disc or DB.