Non-synced objects alongside synced objects

Is it possible to have some realm objects that are not synced and other realm objects that are synced in the same app? Or do they all have to be either synced or not? Thanks for any help that you can offer.

Hello Deji_Apps! Welcome to the community!

So short answer, yes.

Long answer, you need to have two separate realms, one realm that is synced which is going to have the objects linked that you want to have synced, and another realm that’s local only and not synced, which will hold the objects that are not intended to be synced to the cloud.

Does that make sense?

Regards,

Brock.

1 Like

Yes, easy. here’s some high level code.

A non-sync’d realm will be accessed like this

let realm = try! Realm()

That line will talk to the local realm file called default.realm. You can add a config to point to a file other than default.realm; like this Realm(config: some config) where config contains a fileUrl you want to use as your local storage.

For sync’d realms, accessing Realm is a combination of a couple of steps, including defining the partition key of the Sync’d Realm you want to access, and a user var containing the user you authenticating with… At a high level it will look more like this for the initial connection - .async open gets the ball rolling

let app = App(id: YOUR_REALM_APP_ID)
// Log in...
let user = app.currentUser
let partitionValue = "some partition value"
var configuration = user!.configuration(partitionValue: partitionValue)
Realm.asyncOpen(configuration: configuration) { result in...

then later on in your app, you’ll access the realm via a config containing the partition (instead of the fileUrl for local files)

let realm = try! Realm(configuration: configuration(partitionValue: partitionValue))

Partition vs Realm is a little confusing to start with but with Sync’ing a Realm is defined by its Partition value whereas with local files a Realm file is a Realm file and does not necessarily contain a partition.

2 Likes

Thank you for the reply!!!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.