Integrating Realm with Xamarin (Includes Realm Sync)

Integrating Realm with Xamarin (Includes Realm Sync)

When it comes to mobile applications with database support, the first thing that comes to mind is SQLite. However, there are a few drawbacks with SQLite such as no support for data encryption and low performance when dealing with large data.  

In this article, we take a closer look at Realm, a non-relational database and a solid replacement for SQLite and core data. It is quite efficient and works seamlessly even with a large amount of data.

Why use Realm

Ways of using Realm 

In this article I will show how to integrate realm in the Xamarin Forms project. I will also show how to use realm as local database(Part-1) and realm cloud(Part-2


Part-1: Using Realm Local Database 

Go to PCL project>—>Right click—>Add—>New File—>XML File—>FodyWeavers.xml 

Go to Android project>—>Right click—>Add—>New File—>XML File—>FodyWeavers.xml 

Go to iOS project>—>Right click—>Add—>New File—>XML File—>FodyWeavers.xml 

Add the below code in the file 


Let’s create a model 

Opening Realms  

Opening a Realm can be done by instantiating a new Realm object.  

Note: If nothing is passed, it creates the default realm. You can add realm name as below 

This will create realm db with “Animals” name 

To add/delete/update or do any operation of realm, first you need a Realm instance. 

1) Add elements 

2) Delete elements 

3) Update elements 

Part-2: Using Realm Cloud 

Now let’s see how to use realm cloud. The setup is still the same. Here we need a few more additional steps. 

1) Create an account in realm cloud at 

(one-month free trial account).  

2) Create an instance in realm cloud by clicking on create an instance 

This instance ID will be further used for authentication  

There are two types of Realm sync 

Here I am going to show you how to use full sync as Realm recommends using Full sync. 

Client Authentication:  

Realm supports two types of authentication 

The below code shows how to use simple authentication with username and password. 

To authenticate, you must supply a server URL. This is the base URL for your server, such as 

Note: Here “myinstance” would be the instance id that you generated while creating your account  

Create Realm through realm studio or through code. If the realm has not been created, while calling the realm instance it will automatically create. If you have already created through realm studio, you can just call the instance. 


Here the realm path consists of instanceId followed by realm name. Something like this 

public static string realmPath = “realms://instanceId/Animals; 

Now let’s login and call realm instance 

//Call the login method

//Call or create realm instance: 

public static async Task GetDataFromRealm() 

In the above sections, we have seen how to integrate realm local db and realm cloud into your xamarin forms project. You will also be able to perform operations like adding items, updating items and deleting items (showed in Part-1). These code snippets are the same for realm local database and realm cloud. 

You can find how to use linq queries in the below reference link. However, few features are not supported such as: groupby, union, intersect, distinct,except,  select, selectmany, join, group join, concat, skipwhile, take, skip and takewhile.  

Note: Opening the realm instance asynchronously for the first time is recommended. Once the data is synchronized, the instance should be called synchronously.  

You can also download realm studio, create a realm file, and add the data. This helps to minimize the code in your application.   

Below are a few reference links that might help with further information 

  • Easy to use 
  • Provides data synchronization 
  • Fast and Efficient 
  • Provides Encryption of data 
  • Using realm locally: This creates a local file in your mobile app. 
  • Using realm cloud: The data lives in realm cloud and will be synchronized with the local database. 
  • Visual Studio 2015 Update2 or higher for windows 
  • Visual Studio for Mac 7.0 or higher 
  • Firstly, create a Xamarin Forms project in Visual Studio for Windows or Mac. 
  • Go to the NuGet packages and install the realm package in all your solutions. Always prefer to install the latest one. 
  • It automatically adds fodyweavers.xml. If not added, please add it manually in all your projects.  
  • Full Sync: All the data present in the cloud will be synced locally 
  • Query Based Sync: Only partial data i.e the result set that is queried will be synced into the local copy. 
  • Simple Authentication: To simply get started one can use simple authentication which uses username, password and other one is anonymous login. 
  • JWT Authentication: It is recommended to use JWT Authentication for a better experience. 
  • Using realm with xamarin: 
  • Linq support: 
  • Realm Encryption: 
  • Research & References of Integrating Realm with Xamarin (Includes Realm Sync)|A&C Accounting And Tax Services

    Leave a Reply

    Related Post