Configuring SQLite for Local Data Storage

Page
edited by
Nicholas Lie

This page discusses things that require attention to use SQLite for local data storage in Crosslight applications.

The common local storage available in most mobile platforms is SQLite. Typically, each platform uses different way to perform data operation to SQLite which increasing technical difficulties and complexity when developing mobile apps. Thanks to the comprehensive and intuitive API, now you can use Crosslight SQLite to manage your local data storage elegantly. To learn more about Crosslight SQLite, see Local Data Access.

Add SQLite PreserveAssembly Configuration

On each platform, Crosslight has AppInitializer.cs which is useful to initialize platform-specific functions. During build process, the linker of Xamarin SDK will remove unused class references, including SQLite class reference. To prevent the Xamarin SDK from removing the references, you will need to add an additional configuration in InitializeServices method of AppInitializer class such as shown in the following. The configuration for each platform is as simple as:

iOS – SQLite PreserveAssembly
public void InitializeServices(IApplicationHost appHost)
{
    UIApplicationDelegate.PreserveAssembly((typeof(Sqlite.ServiceInitializer).Assembly));
}
Android – SQLite PreserveAssembly
public void InitializeServices(IApplicationHost appHost)
{
    AndroidApp.PreserveAssembly((typeof(Sqlite.ServiceInitializer).Assembly));
} 
WinPhone – SQLite PreserveAssembly
public void InitializeServices(IApplicationHost appHost)
{
    AndroidApp.PreserveAssembly((typeof(Sqlite.ServiceInitializer).Assembly));
} 
WinRT – SQLite PreserveAssembly
public void InitializeServices(IApplicationHost appHost)
{
    Application.PreserveAssembly((typeof(Intersoft.Crosslight.Data.SQLite.ServiceInitializer).GetObjectTypeInfo().Assembly));
} 

By adding this configuration, Xamarin SDK will treat SQLite class reference as in-used, so the SQLite will now run well.

Ensuring Windows Phone SQLite Assemblies

Windows Phone application development can only be done in Windows using Visual Studio, so the following steps is only applicable for Windows Phone development using Visual Studio 2012 (or above).

Check Visual Studio Gallery to install the correct version of SQLite extension for Windows Phone.

 

By default, Visual Studio does not provide SQLite extension to utilize SQLite in Windows Phone. Follow the steps below to add SQLite to a Windows Phone application:

  • Install SQLite for Windows Phone in Visual Studio 2012 (or above) extensions and updates. From Visual Studio, go to Tools –> Extensions and Updates.
  • After installed, right-click on the Windows Phone project and choose Add Reference…
    In the Reference Manager select the Windows Phone SDK 8.0 section on the left, then tick SQLite for Windows Phone from the Extensions list.

The Windows Phone project will now have SQLite listed in the project References.

Leave a Reply