Xamarin

Xamarin

Edit on GitHub
Platform: iOS Android WPF

Add Couchbase Lite to your app

NuGet

  1. In Xamarin Studio, right-click on the desired project and select the menu to add a Nuget dependency.

  2. Search for 'Couchbase Lite' and select the latest version of the Couchbase.Lite package.

  3. Once Couchbase Lite is installed you can run the project with Couchbase Lite as a dependency.

Available packages

The following libraries can be used in a Xamarin Studio project to import different components.

Package Name Server
Couchbase.Lite.Storage.SystemSQLite To use SQLite as the storage type. This is the default storage type.
Couchbase.Lite.Storage.SQLCipher To use SQLCipher as the storage type.
Couchbase.Lite.Storage.ForestDB To use ForestDB as the storage type.

Getting Started

Create a new Xamarin Forms project in Xamarin Studio. Make sure to select the Use Shared Library option.

Right-click the .iOS project and add the Couchbase Lite dependency as a Nuget package. Repeat the same steps for the .Droid project.

Open App.xaml.cs in Xamarin Studio and add the following in the OnStart method.

/* This line is required for iOS only */
Couchbase.Lite.Storage.SystemSQLite.Plugin.Register();

/* Create a manager */
Manager manager = Manager.SharedInstance;

/* Create or open the database named app */
Database database = manager.GetDatabase("app");

/* The properties that will be saved on the document */
Dictionary<string, object> properties = new Dictionary<string, object>
{
    { "title", "Couchbase Mobile"},
    { "sdk", "C#" }
};

/* Create a new document */
Document document = database.CreateDocument();

/* Save the document to the database */
document.PutProperties(properties);

/* Log the document ID (generated by the database)
   and properties */
Console.WriteLine($"Document ID :: {document.Id}");
Console.WriteLine($"Learning {document.GetProperty("title")} with {document.GetProperty("sdk")}");

/* Create replicators to push & pull changes to & from Sync Gateway. */
Uri url = new Uri("http://localhost:4984/hello");
Replication push = database.CreatePushReplication(url);
Replication pull = database.CreatePullReplication(url);
push.Continuous = true;
pull.Continuous = true;

/* Start replicators */
push.Start();
pull.Start();

Build and run. Notice the document ID and property are printed to the console. The document was successfully persisted to the database.

Installing Sync Gateway ⟶