@AsyncOpen + partition value

hey everyone, I am currently working with realm sync and use partition based app service.

on my main screen I have x items with different partitionValue. when I select an item I am changing .environment(\.partitionValue, newValue). this is working for the first start.

if I go back to this list and select the same item again, I am able to open the view again, but my sync is disconnecting.

if I select a different one, my app is crashing and saying already opened with different partitionValue

1.st question: is there a way to not disconnect from sync when I am opening again the same AsyncOpen view?

2.nd question: how am I allowed to remove the partition to setup a different one for my view with @AsyncOpen

Can you clarify what you mean by ‘sync is disconnecting’? Realm data is always stored locally first and then Realm sync’s it to the server at a later time - the sync connecting, sync’ing and disconnecting is a normal process.

On the partition part of the question; if you need to change partitions, simply set your results to nil (if you store them as a class var), and then async open with the new partition and load the new results.

i am connecting to my sync database and open partition value “xyz”

2022-08-17 09:38:53.436796+0200 XYZ[xxxxx: xxxxx] Sync: Connected to endpoint 'ip.address' (from 'ip.address')

after I leave my view with @AsyncOpen wrapper, and go back to the same partition, this is what I receive:

2022-08-17 09:38:59.890651+0200 XYZ[xxxxx: xxxxx] Sync: Connection[5]: Disconnected

reagarding my point to switch partition, i don’t store any realm locally, we just use @AsyncOpen and set .environment(\.partitionValue, value)

Realm is an offline first database.

Realm Database is offline-first. You always read from and write to the local database, not over the network. When Device Sync is enabled, Realm Database synchronizes data with App Services over the network in a background thread. The sync protocol resolves conflicts consistently on each client and in the linked Atlas cluster.

Are you perhaps accessing Atlas directly using the Swift Driver?

ok thats right, but I am a bit confused why its disconnecting when we select it for the 2nd time - on 1st selection, its not disconnecting (seems to me that there is something wrong)

not sure what you mean with swift driver?

I am only logging in on app start app.login(credentials:) and after I am logged in I am selecting my partition.
there is a view which has @AsyncOpen property and I am providing to that view my partitionValue.

but like already explained, once I want to switch from partition xyz to partition yza its crashing with error already opened with another partition - but I don’t have any realm property anywhere.

am I forced to use realm instances in a manager or something like that and provide different realms to my views? I was thinking that the @AsyncOpen wrapper will manage this for me.

You don’t have control over connecting and disconnecting - that’s not how Realm works; it does all of that for you. Your focus is to write the data to Realm and then it does the heavy lifting in the background when it syncs.

AsyncOpen is used to ‘tell’ Realm you’re using Sync, instead of only writing locally. It establishes the initial connection and provides a status so you can take action while it’s connecting - progress bars, user auth and then to let you know it’s ready for use.

There is an option to read/write directly to Atlas from Swift (not using Realm) but I don’t think that’s what you’re doing.

In general, the async function would look something like this

let config = user.configuration(partitionValue: whichPartition)
Realm.asyncOpen(configuration: config) { result in
    switch result {
    case .failure(let error):
        print("Failed to open realm: \(error.localizedDescription)")
    case .success(let realm):
        print("Successfully opened realm: \(realm)")
        //do something with realm - maybe query for tasks and store them in a class var

when switching partitions, if there’s a class var holding our realm data we nil it

self.taskResults = nil

and then pass a new partition to the above code whichPartition which then connects to the new partition. That process works well.

Perhaps your process for switching partitions needs to be investigated.