James Montemagno
James Montemagno

Live, Love, Bike, and Code.

Tags


Twitter


Updating Azure Mobile SQLiteStore to 3.0

James MontemagnoJames Montemagno

If you are using Azure Mobile Apps as a backend for your Xamarin and Windows apps you may have seen a very fancy new 3.0 release pop up in NuGet. This is a super important update for the NuGet that brings in the amazing SQLite.raw to ensure compatibility with changes in Android N. So what is new? Well according to the changelog:

Version 3.0.0

Doesn’t seem too crazy, but why the 3.0? Well it is because there is a breaking change with the update to SQLite.raw and how you have to initialize SQLite on multiple operating systems. I highly recommend reading the SQLite.raw README to fully get detail, but here is the short:

UPDATE! 3.0.2 Fixes Everything!

After providing the team with much feedback on the first 3.0 and 3.0.1 release they have fixed the issue I blogged about for the greater good! You can find details here on their blog, but the overview is that you wont have to modify really anything into your app. You should still uninstall the old SQLitePCL and remove the SQLitePCL.Init() that you may have had in the past, but besides that the correct files will be created for you automatically! Yay!

All you need to do is specify your path like this:

var path = "syncstore.db";
path = Path.Combine(MobileServiceClient.DefaultDatabasePath, path);

In the future, I am hoping that you can just specify the path without the combine. I have already filed an issue :)

Below is only for 3.0.1 Reference

Call SQLitePCL.Batteries.Init();

Previously, on iOS you would have to call SQLitePCL.CurrentPlatorm.Init(), but now, you must call into Batteries to initialize it on ALL platforms;

//Call this in each platform before intializing your Mobile Client
SQLitePCL.Batteries.Init();

Android Has a Special Path for Database

For some reason… not sure why, but you have to set a special path for Android specifically. I really think that the library should do this, but it doesn’t so you need to get the path correctly for Android such as:
var path =  "syncstore.db";

#if __ANDROID__
path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), path);

if (!File.Exists(path))
    File.Create(path).Dispose();
#endif

You won’t be able to do this in your PCL, but if you are using shared code projects this will work, else you will have to pass it up to your PCL.

This means that in your PCL project you should do something like this:

public class MyAzureService
{
  public static string Path {get;set;} = "syncstore.db";
}

Then in your MainActivity before your app starts up simply execute this code:

MyAzureService.Path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), MyAzureService.Path);

if (!File.Exists(MyAzureService.Path))
    File.Create(MyAzureService.Path).Dispose();

If you want to take it a step further you could create an interface and use the DependencyService or something of this sort to inject it.

See it in action

I just updated my CoffeeCups app to 3.0.1 and also take a look at the new Quick Start guides.

Live, Love, Bike, and Code

Comments