How to preserve cache value when the application is in the background
Basic scenario
We use Akavache 6.8 as our cache (using InMemory) in our Xamarin mobile project, it works okay under normal circumstances.
Basically we use InserObject calls to put data in the cache and used GetOrFetch for database entities and GetObject for memory-only cached key values - because of the problems we later changed to GetOrCreate.
Problems
When the application goes in the background and I try to use it again after some time, the cache retrieve methods return nothing. As we changed to GetOrCreate, there are no errors on retrieve but we can't use the default (null or empty) values.
Code properties
As I read your guides, I might have found some suspected problem source but I need some guidance of where to look.
We initialize the BlobCache in the main application file as a singleton using MvvmCross dependency injector container:
// Make sure you set the application name before doing any inserts or gets
BlobCache.ApplicationName = nameof(LOBApplication1);
Mvx.IoCProvider.LazyConstructAndRegisterSingleton(() => BlobCache.InMemory); //lazy singleton
In your guide there is a specific part that mentions the use of Shutdown when the application shuts down.
We don't use that for a few reasons:
- We don't use Akavache as persistent store, only as a cache
- There is no traditional 'shutdown' in a mobile application - it simply goes in the background and sometimes the OS or the user wipes it from the memory
- The transition of queued items is not critical, but loosing the already cached values is a pain
So we skip that but I can't be sure of the implication of the lacking of Shutdown in an Inmemory-only scenario.
I'm also not sure if any call to Flush would help the case. What would it cause in a memory-only setup? Where should I call it? When the application goes to the background?
Can you guide me how I should stabilize the cache in our application?
I'm also confused from the docs how one should/shouldn't shutdown the cache OnSleep, and if so how to recover back to a functioning state when the app wakes back up.