How can I set up subscriptions to queries on queryable fields with Realm and MongoDB?

150 views Asked by At

I am trying to make a cross platform desktop app, which needs to be available offline with online sync.

So I have set it up using .NET Maui and MongoDB, connecting using Realm. I have never used Realm before, so I started with the tutorial to-do app at https://github.com/mongodb/template-app-maui-todo The tutorial app works fine, but I am trying to get a better understanding of the concept of subscriptions, and I have not found a resource that lets me understand how to set this up correctly.

The tutorial writes Item objects to MongoDB. Instead, I want to write Story objects to MongoDB in the same way, but I get the error:

**Cannot write to class StoryModel when no flexible sync subscription has been created. **

The error itself is very clear, and I can even see from my RealmService, that there is still a reference to Item in my subscriptions:

enter image description here

However, I do not know where this reference comes from. How are the subscriptions set up, and how is the subscription.ObjectType populated?

I have searched through my code to strip out any reference to Item, but there are none, so I am very confused about how the subscription is meant to be set up and what I need to alter.

Apologies for the crude question. I am very much a beginner at this type of programming. Obviously happy to provide further details and code samples. I'm just currently not sure which code samples are useful to look at.

1

There are 1 answers

0
Fred Roaldset On

Answered in comments above. Summarizing here:

Generally speaking if you define a Realm object, use it, and then delete it from code, the object will "live on" both locally and if syncing, in Atlas as well. During development, we frequently delete our Realm file and allow a new one to be created - also deleting the object from the Atlas server as well. Same with subscriptions - once they are created, they live on and can be referred to by name.

In dotnet C#, use realm.Config.DatabasePath to see where the realm file is located. On Windows, if not set differently, it is at C:\Users<username>\Documents\default.realm along with default.realm.management folder and default.realm.lock

I deleted this file, and it solved my issue.