Convert SQLite MAUI app to Realm with Device Sync

Hi, I’ve fairly recently started writing a MAUI app that is using SQLite with Azure SQL in the back end. Azure SQL was always very temporary to allow the API’s to be written and we will be replacing that with a MongoDB Atlas database any time now. I watched Luce Carter’s great presentation in the MAUI conference last week and realised that Device Sync using Realm is perfect for our application. Are there any write ups or tutorials on replacing SQLite or at least adding Realm and Device Sync to an existing MAUI app? There is currently very little SQLite in the app so I’m happy to manually convert that to Realm. I’m just not sure where to start with adding Realm and setting up Device Sync and the MongoDB database. I’ve got as far as adding the Nuget packages. Thank you.

This is a great article by our own @papafe which goes over how to use Realm effectively in a Xamarin.Forms app. Considering MAUI is mostly an evolution of XF, a lot of the concepts in the article apply to MAUI apps.


Thank you. I’ll take a look.

That was a very good and useful article. It shows that it should be relatively simple to change my app from SQLite to Realm because it is using the MVVM pattern and making use of DTO’s.
Useful as this article is, it shows that my question could have been better and was in the wrong forum category. My initial problem is in setting up Realm Sync where I already have models in my app. I see the author of the article says there will be a follow-up showing how to use Realm Sync.
Perhaps it is just a case of creating an app in App Services, getting the AppId for use in the app and then the initial sync will automatically create the required collections in MongoDB.

Hey John,

So glad to hear you enjoyed my talk and were inspired to move to Realm!

I wasn’t able to talk through Sync and how to handle this kind of use case (it is very common) as much as I would have liked due to time.

But my advice is to do as you suggested and setup Sync. Atlas will create documents and collections, if they don’t exist, the first time you add documents. If you leave Developer Mode on, it allows your client data to define the structure of your documents as documents are added. This is to support the ever changing world of prototyping. It will handle your existing models no problem.

Once you are happy that the documents match the schema that you want because your models are finalised and you have some data in your Atlas collection, you can turn Developer Mode off. This requires a schema but Atlas can generate that for you based on the existing data in the collection so really easy!


Hi Luce, Thank you for the useful answer. I’ll do as you suggest and no doubt I’ll report back or have further questions. Special thanks though for your talk at the conference. Without that, I would not have known about Realm or Sync or how easily it can be used in a MAUI MVVM app. The company I work for are already starting to use MongoDB and it now looks like it is a perfect fit with mobile apps.

Hi @Luce_Carter
I’ve made a little progress with this but I seem to have something configured wrongly in App Services. I suspect it is to do with me choosing Flexible Sync rather than Partition Based. In fact, I get the same problem if I run your HouseMovingAssistant app (with my AppId) as when I run my own app. Yours, of course, was configured for Partition Based, and my App Services is set to Flexible.
The inner exception I get in both apps is
Wrong wire protocol, switch to the flexible sync wire protocol
so that pretty much says it all! What I don’t know is how to change my app so it is using Flexible Sync. My code was copied from yours with the exception on the second line of this:

config = new PartitionSyncConfiguration($"{App.RealmApp.CurrentUser.Id}", App.RealmApp.CurrentUser);
realm = await Realm.GetInstanceAsync(config);


Hey John, if you want to use flexible sync on the backend, you should open a Realm using FlexibleSyncConfiguration:

config = new FlexibleSyncConfiguration(App.RealmApp.CurrentUser);
realm = await Realm.GetInstanceAsync(config);

// Add subscriptions and read/write data

Be sure to check out the docs for adding subscriptions.

Thank you Nikola. I got there a few minutes before your post!

Sorry to have wasted your time with such a basic question. It is starting to make sense now. I will check out adding subscriptions.


No worries, we’re here to help :slight_smile:

1 Like