Watch keynotes and sessions from MongoDB.live, our virtual developer conference.

Opening Sync'd Realm Sequence

The getting started guide Open a Synced Realm indicates the first time a realm is opened, it should be with asyncOpen.

Realm.asyncOpen(configuration:

The reason is that the normal Realm() initializer is a write and creates the database schema which would fail on a Read Only realm.

If Realm.asyncOpen is used the first time, but then the normal initializer is used thereafter

realm = try! Realm(configuration:

would that still not be a write? Should synced realms only be opened with .asyncOpen?

What’s the proper process here - should .asyncOpen be tossed into the AppDelegate or app opening sequence?

The documentation implies this applies to all realms?

If it is the first time opening that particular realm, especially if it is a read-only realm

but then says specifically for read-only

the first time you open any given read-only realm.

Any guidance is appreciated.

No it will not be a write because after the first initial asyncOpen, the try Realm method will use the cached file on disk.

I would toss it in the initial app opening sequence but probably not in AppDelegate, in your first initial VC after logging in. asyncOpen is typically tied to some initial loading screen and you can tie progressListeners to it to show the user that data is downloading.

1 Like

@Ian_Ward

Just saw the new 5.1 release which has a bit of a behavior change/addition from the above:

  • Allow opening full-sync Realms in read-only mode. This disables local schema initialization, which makes it possible to open a Realm which the user does not have write access to without using asyncOpen. In addition, it will report errors immediately when an operation would require writing to the Realm rather than reporting it via the sync error handler only after the server rejects the write.

So read-only realms can now be accessed without asyncOpen.

2 Likes

It sure does Jay - glad to see you are following out releases!

2 Likes

This change is great news! Makes it a lot easier to work with readonly realms :slight_smile:

1 Like