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 USE_SQLITEPCL_RAW conditional variable in the shared project


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 uri = "file:test.db?node=primary&bind=tcp://0.0.0.0:1234";
var db = new SQLiteConnection(uri);

// check if the db is ready
while (true) {
    var status = db.ExecuteScalar<string>("pragma sync_status");
    if (status.Contains("\"db_is_ready\": true")) break;
    System.Threading.Thread.Sleep(250);
}

// now we can use the database
...




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());
            }

            // open the database
            var uri = "file:test.db?node=primary&bind=tcp://0.0.0.0: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
            ...
        }
    }
}