Opening Realm: how prevent asyncOpen for a query based sync to load all data of all classes?


#1

In Realm Sync Documentation (https://docs.realm.io/sync/using-synced-realms/setting-up-your-realms#opening-realms), I read:

We also recommend using synchronous open for all large data sets that are not contingent on the functioning of the app. For instance, in a retail app, you may want to asyncOpen the types of products, the locations of stores, and other business logic data, but the actual inventory of clothing should be synchronously downloaded in the background by the Realm process while the UI updates continuously.

Given the example above, I have a query based synced realm for a retail app with classes with small data sets like types of products, locations of stores, which I want to make available with all its contents right from the app start using asyncOpen. And then, I have classes with big data sets, like inventory, which I only want to load on demand (synchronously).

Given, all these classes live in the same realm (because they are in relationships), how can I tell asyncOpen which classes to load?


#2

You’ll need to use query-based sync to each class of data. Use callbacks from each sync query to update the UI as new information comes in. Async or synchronous open makes no difference as far as syncing data from the server: both will sync no objects until you create the subscriptions.


#3

So, executing this code doesn’t load any data?

let config = user.configuration(realmURL: realmURL))
Realm.asyncOpen(configuration: config) { realm, error in
    if let realm = realm {
        // Realm successfully opened, with all remote data available
    } else if let error = error {
        // Handle error that occurred while opening or downloading the contents of the Realm
    }
}

#4

Not any of your product types, stores, or inventory, no.

By default, a query-based synchronized Realm contains no data. Instead, the client application must choose, or subscribe to, which subset of data in the corresponding Realm on the server it wants to synchronize.

https://docs.realm.io/sync/using-synced-realms/syncing-data#subscribing-to-data-reading-data