I'm creating an iOS application, where I intend to provide data sync across device feature, only to the premium users. I find Realm Sync as a good solution to keep the local on-device database and cloud MongoDB Atlas in sync. However, I don't want to sync the data of the non-premium users to the cloud database.
I'm enlisting a couple of ways that I can think of to prevent Realm Sync from triggering for non-premium users, but I'm not sure on what is the best way for this problem.
Prevent syncing by leveraging Sync permissions - I can store list of premium user ids and only give sync permissions to those users.
{ "%%user.id": [ "5f4863e4d49bd2191ff1e623", "5f48640dd49bd2191ff1e624", "5f486417d49bd2191ff1e625" ] }
Configure Realm objects on client side i.e. only allow all Realm objects / models if the user is premium.
// Get a configuration to open the synced realm. var configuration = user.configuration(partitionValue: "user=\(user.id)") // For non-premium user it would be [User.self] configuration.objectTypes = [User.self, Project.self] Realm.asyncOpen(configuration: configuration) { [weak self](result) in /*...*/ }
I'm looking for insights / recommended approach to this problem.
Edit
I've a few additional questions about handling two use cases differently - non-premium one by opening a local only Realm()
and the premium one with Realm.asyncOpen()
.
- How to handle a use case when an existing user switches to a premium subscription? Should calling
Realm.asyncOpen()
suffice or do I need to do any special handling? - I plan to sync all my
User
(custom document in a collection) records for all users (premium + non-premium). My guess is I should open a normal Realm for all my conent and synced Realm with just[User.self]
object in the configuration.