Syncing some but not all of the collections in the database

I am using Realm Flexible Sync on iOS. My app has 5 collections, but I only want to sync 2 of them to the cloud, is it possible? Or I have no choice but to sync ALL the collections? It seems that if I don’t define a subscription, I will not be able to save any data to the collection. Thanks!

Hey Richard - if you only want to sync 2 of your 5 collections, you would need to use separate realms; a synced realm to manage the object types you want to sync, and a non-synced realm for the non-synced objects. There is no way to write objects to a synced realm that you don’t want to sync.

Thanks so much for your response. Could you point me to the documentation that explains how to use multiple realms within the same app? Thanks!

P.S. Using objectTypes in configuration?

Yes, you’ve got it - to use multiple realms in an app you would need to use objectTypes in the realm configuration to manage the objects in each realm.

Unfortunately we don’t have any documentation that deals specifically with using multiple realms within the same app. We’re hoping to add more guidance in the docs in the future, but I don’t have anything to point you at right now.

Conceptually, you just initialize both realms with different variable names and just read/write from the one you want wherever you need those objects. If you have a specific question, though, I’m happy to try to answer it.

Will definitely try that out. Thanks for the great help!

When I am just using the local realm, I don’t need to define anything, ObservedResults will automatically bring in all the objects in the collection.

Now when I have both a local and a sync realm, I am not sure exactly what I should do. Should I define two realms at the app level - a localRealm with objectTypes of the local collections, and a syncRealm with objectTypes of the sync collections. And then pass them as environment objects to the child views? So in those child views, I can continue to use ObservedResults with the collection name (no matter local or sync)? A view needs to observe both a local and a sync collection. I am lost…

P.S. When the app is first run, Realm will look at my data models and create the default.realm, how would it know which collection(s) to include when I have a local and a sync realm?

One thing you’ll run into - realm’s default environmentObject implementation is to use one realm as an environment object. If you want to use more than one realm as an environment object, you might have a look at this similar thread - it discussed opening two synced realms, which isn’t your use case, but there’s some good advice there about using more than one realm as environment objects: Help on opening multiple Synced Realms (SwiftUI) .

I personally haven’t tried to work through this use case before, so I don’t know off the top of my head what this looks like in practice. But in theory, yes, if you define two realms - a non-synced Realm that manages the object types you don’t want to sync, and a synced realm that manages the object types you do want to sync, you should be able to use them wherever you need to access those objects. I don’t think there’s anything wrong with having a view observe more than one ObservedResults collection - those are bound to the collection type anyway so if your app has 5 objects, you could have 5 ObservedResults observing each of those types of objects. But, again, I haven’t tried to do this in practice, so I’m not sure what the actual behavior would be on doing this.

For the default realm, you can explicitly define one of your realm configuration to set which is the default realm. These docs show how to use the defaultConfiguration parameter to specifically define a default realm: Open a Default Realm