Xamarin

There are 2 options to use LiteSync on Xamarin:


SQLite.NET

The SQLite.NET for LiteSync is a fork of SQLite.NET that uses the LiteSync library


Instructions

First follow the instructions to compile LiteSync for mobile or use the pre-compiled native libraries.

You can start downloading the free version:

Android   iOS

Add the native libraries to the project according to the platform:


Android

On Android you may have a folder structure like this:

- lib

- arm64-v8a

- armeabi-v7a

- x86

- x86_64

On each sub-folder you may have 3 libraries:

liblitesync.so

libuv.so

libbinn.so

For each of the .so files, CONTROL + CLICK then set Build Action to AndroidNativeLibrary


iOS

On iOS we should have 3 fat libraries for static linking:

liblitesync.a

libuv.a

libbinn.a

CONTROL + CLICK on the Xamarin.iOS project then choose Add > Add Native Reference and select the 3 libraries from the folder

CONTROL + CLICK on each library, click Properties and enable these items:

√ Force Load

√ Smart Link


Nuget Packages

Install with these commands:

dotnet add package SQLitePCLRaw.core --version 2.0.4

dotnet add package SQLitePCLRawProvider.static --version 2.0.4

dotnet add package SQLitePCLRawProvider.LiteSync --version 2.0.4


Finally

Add the SQLite.cs file to your shared project

And define the __MOBILE__ and USE_SQLITEPCL_RAW conditional variables in the main/shared project



Tables

The library contains simple attributes that you can use to control the construction of tables

public class TodoItem
{
    [PrimaryKey]
    public long ID { get; set; }
    public string Name { get; set; }
    public bool Done { get; set; }
}

On tables with integer primary keys the primary key column must be declared as long instead of int because LiteSync always use a 64-bit number for row ids

LiteSync does not support the AutoIncrement keyword


Example Code
using SQLite;
using SQLitePCL;

if (Device.RuntimePlatform == Device.iOS)
{
    // use the LiteSync static native library
    SQLitePCL.raw.SetProvider(new SQLite3Provider_static());
}
else // if (Device.RuntimePlatform == Device.Android)
{
    // load the LiteSync dynamic native library
    SQLitePCL.raw.SetProvider(new SQLite3Provider_LiteSync());
}

// open the database
var databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "App.db");
var uri = "file:" + databasePath + "?node=secondary&connect=tcp://123.45.67.89:1234";
var db = new SQLiteConnection(uri);

// wait until the db is ready
while (!db.IsReady()) {
    System.Threading.Thread.Sleep(250);
}

// now we can use the database
db.CreateTable<TodoItem>(CreateFlags.AutoIncPK);
...

Instead of waiting until the database is ready for access we can use an event notification:

// callback for when the db is ready (do not access the database before)
db.OnReady(() => {
    // the database is ready to be accessed
    db.CreateTable<TodoItem>(CreateFlags.AutoIncPK);
    ...
});

Your application can also receive a notification when the database receives a sync/update:

db.OnSync(() => {
  // the db received an update. update the screen with new data
  UpdateScreen(db);
});

To retrieve the synchronization status:

var status = db.ExecuteScalar<string>("pragma sync_status");



Microsoft.Data.SQLite

The Microsoft.Data.SQLite uses the SQLitePCLRaw to interface with the native library


Native Library

First follow the instructions to compile LiteSync for mobile or use the pre-compiled native libraries. You can start downloading the free version:

Android   iOS

Add the native libraries to the project according to the platform:


Android

On Android you may have a folder structure like this:

- lib

- arm64-v8a

- armeabi-v7a

- x86

- x86_64

On each sub-folder you may have 3 libraries:

liblitesync.so

libuv.so

libbinn.so

For each of the .so files, CONTROL + CLICK then set Build Action to AndroidNativeLibrary


iOS

On iOS we should have 3 fat libraries for static linking:

liblitesync.a

libuv.a

libbinn.a

CONTROL + CLICK on the Xamarin.iOS project then choose Add > Add Native Reference and select the 3 libraries from the folder

CONTROL + CLICK on each library, click Properties and enable these items:

√ Force Load

√ Smart Link


Nuget Packages

Install with these commands:

dotnet add package Microsoft.Data.Sqlite

dotnet add package Microsoft.Data.Sqlite.Core

dotnet add package SQLitePCLRaw.core --version 2.0.4

dotnet add package SQLitePCLRawProvider.static --version 2.0.4

dotnet add package SQLitePCLRawProvider.LiteSync --version 2.0.4


Example Code

Make sure to load the native library before using Microsoft.Data.Sqlite

using Microsoft.Data.Sqlite;
using SQLitePCL;

namespace LiteSyncExample
{
    class Program
    {
        static void Main()
        {

            if (Device.RuntimePlatform == Device.iOS)
            {
                // use the LiteSync static native library
                SQLitePCL.raw.SetProvider(new SQLite3Provider_static());
            }
            else // if (Device.RuntimePlatform == Device.Android)
            {
                // load the LiteSync dynamic native library
                SQLitePCL.raw.SetProvider(new SQLite3Provider_LiteSync());
            }
            SQLitePCL.raw.FreezeProvider(true);

            // open the database
            var databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "App.db");
            var uri = "file:" + databasePath + "?node=secondary&connect=tcp://123.45.67.89:1234";
            var db = new SqliteConnection("Data Source=" + uri);
            db.Open();

            // check if the db is ready
            while (true) {
                SqliteCommand cmd = db.CreateCommand();
                cmd.CommandText = "pragma sync_status";
                string status = (string) cmd.ExecuteScalar();
                if (status == null) {
                    Console.WriteLine("the loaded library does not contain LiteSync");
                    return;
                }
                if (status.Contains("\"db_is_ready\": true")) break;
                System.Threading.Thread.Sleep(250);
            }

            // now we can use the database
            ...
        }
    }
}