Akavache serious problem after data is cleared
Today I just found a serious problem with Akavache that somehow seems to be overlooked by everyone. Akavache is written by Paul Betts whose 2 of his many other contributions include refit and ModernHttpCLient, both of which I’m using to build my Xamarin Forms app (along with Akavache). If you’re building your mobile apps using Xamarin Forms, you would likely, just like me (no pun intended), come to know about these 3 great libraries thanks to an excellent post “Resilient network service with mobile Xamarin apps” by Rob Givens. In his post, Rob Givens also recommends a great Akavache overview article “Akavache is AKA Awesome” by Matthew Soucoup. Tim Sneed also talked about Akavache. Walking on shoulders of these giants, I studied about Akavache including their sample demos on github:
Then I got my app working great both on iOS and Android. I was happy.
But then today I discovered that on Android (have not tested on iOS yet) that if I go to the app settings and clear the data and cache, my app and ResilientServices, AkavacheDemo and AkaAwesome above would just crash. The apps would start momentarily and just silently disappear.
Here’s what I think happens. When we deploy the apps to the phones, the backing SQLite database was deployed as a bundle. Akavache assumes the SQLite db is there and everything works. After the app data is cleared, at initialization time, even before the apps try to read or write anything, Akavache couldn’t find the database and it just throws some unhandled exception or something like that. The point is the apps just crash by silently disappearing.
Here’s what I think the apps can do to fix it. At startup, before setting the Akavache.BlobCache.ApplicationName, the apps can check if the backing SQLite database is there. If not, the apps can create them. If I read the code correctly, the db for LocalMachine is called blobs.db, for UserAccount userblobs.db, Secure BlobCache secret.db. Luckily, Rob Givens also teaches how to do this. Ultimately, it would be nice if this would be handled by the library Akavache itself.
Update: The problem turned out to be because when I deploy to Android, I have the option “Fast Deployment” enabled. A user/contributor on github helped me figure out that was the cause of this problem that I was seeing. Thanks @flagbug. I wouln’t blame myself for NOT knowing this :-)). Kidding asides, I still think that’s an important point that gets overlooked or under blogged. So this info here might very well help the next person in the future. So remember, when you use Akavache, make sure the deployment to Android has “Fast Deployment” disabled, especially for production builds.