[Xamarin] Getting started with Couchbase Lite!

June 5th, 2015 | Posted by Tom in .NET | Article | Xamarin | Read: 4,948

Couchbase Lite is small, light, document-oriented (No-SQL) JSON database engine available that can be easily used in any Xamarin (iOS / Android) applications, thanks to the dedicated component: https://components.xamarin.com/view/couchbase-lite-net


Using Couchbase Lite is really easy and consists in only a few steps. First, we need to create a database:

var database = Manager.SharedInstance.GetDatabase("Database");

Once the database is created, we can add some documents to it (a document is something similar to a table). Each document is composed of a set of properties:

// Create a document
var properties = new Dictionary<string, object>()
    { "Id", Guid.NewGuid() },
    { "Title", "Getting started with Couchbase Lite!" },
    { "CreationDate", DateTime.Now },

var document = database.CreateDocument();

Of course, later in your application, you’ll be able to retrieve a specific document (via its Id property) to enumerate all its properties:

// Retrieve a document
var alert = new AlertDialog.Builder(this);
alert.SetTitle("CouchBase Lite!");
alert.SetPositiveButton("OK", (sender, args) => {});

var retrievedDocument = database.GetDocument(document.Id);
foreach (var property in retrievedDocument.Properties)
    alert.SetMessage(string.Format("Property Name: {0} - Property Value: {1}", property.Key, property.Value));

A document can, of course, be updated with new properties. The best way to update the document is to get its properties and update the interesting one:

// Update a document
var updatedProperties = new Dictionary<string, object>(retrievedDocument.Properties);
updatedProperties["Title"] = "This is an updated title!";

var updatedDocument = retrievedDocument.PutProperties(updatedProperties);

To delete a document (or a database), simply use the dedicated methods on the objects:

// Delete a document


As I told you previously, Couchbase Lite is very powerful but very simple to use and to add to your application, don’t hesitate to give it a try!


Happy coding!

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

Add Comment Register

Leave a Reply