We are using Akavache on project, in Android everything works great but on iOS we had some issue that is if we save a key value in the cache like this:
public async void Salvar(Login login)
{
await BlobCache.LocalMachine.InsertObject("login", login);
}
BlobCache will save without erros, if i stop the debug session, and start again the debug and try do get the value using:
public async Task<Login> Recuperar()
{
try
{
var dados = await BlobCache.LocalMachine.GetObject<Login>("login");
return dados;
}
catch (KeyNotFoundException)
{
return null;
}
}
i will get the KeyNotFoundException, i dont know why in Android works great but on iOS seeams that the database is gone after restart the app.
in my Xamarin config on Visual Studio check Preserve application data/cache on device between deploys is true and my
BlobCache.ApplicationName = "AppName";
at OnStar Method
Anyone knows what is happening? thanks
Finally solved! the problem is that the path that Akavache is using, after creating my own path the problem is solved.
1 - Create a DependencyService that get your runtime platform path like for iOS:
var documentsPath =Environment.GetFolderPath(Environment.SpecialFolder.Personal);
var libraryPath = Path.Combine(documentsPath, "..", "Library");
var path = Path.Combine(libraryPath, "cache.db");
in your PCL init you do this:
BlobCache.ApplicationName = "AppName";
BlobCache.EnsureInitialized();
var local = DependencyService.Get<ILocalCache>();
var pathToCacheDbFile = local.GetPath();
CacheConfig.NormalCache = new SQLitePersistentBlobCache(pathToCacheDbFile);
My CacheConfig:
public class CacheConfig
{
public static bool Initialised { get; set; }
public static IBlobCache NormalCache { get; set; }
}
then you use all your caches like this:
public async Task<bool> Salvar(Login login)
{
try
{
await CacheConfig.NormalCache.InsertObject("login", login);
}
catch (Exception ex)
{
//todo
}
return true;
}
this solution is based on other threads here.
Regards