realm-cpp
realm-cpp copied to clipboard
Offline usage
Should it currently be possible to use a synced Realm instance offline?
If I run the app while offline for the first time (with no realm created yet), open throws an exception: http error code considered fatal. Server Error: 500.
If restart the app when offline (after realm has been created), it currently seems to block when calling subscriptions.update, reporting this sync error Failed to connect to sync: Host not found (authoritative).
If I go offline after successful startup (while online), and for example create a new document, I then get the sync error but it is non blocking. Is it possible to get this behaviour also when first opening a synced realm instance?
If not, what is the current recommended workaround? Should I manually check and instantiate a local vs synced instance depending on the connection status?
Hi @adamski
Currently for the first scenario mentioned, you will need to check your network status and either open a local realm accordingly and then copy over the data to the synced realm once an internet connection becomes available. You could also use a try-catch block on the app.login to decide which branch to take.
If you already have a synced realm created and the subscriptions.update has ran previously then I would advise not run it again, you can check if a subscription already exists with subscriptions.find. The subscriptions configurations persist.
I would do something like:
First startup:
auto user = app.login(realm::App::credentials::anonymous()).get();
auto flx_sync_config = user.flexible_sync_configuration();
auto synced_realm = experimental::db(flx_sync_config);
auto subs = synced_realm.subscriptions();
if (!subs.find("foo-strings")) {
synced_realm.subscriptions().update([](realm::mutable_sync_subscription_set &subs) {
subs.add<experimental::AllTypesObject>("foo-strings", [](auto &obj) {
return obj.str_col == "foo";
});
}).get();
}
synced_realm.write([&synced_realm]() {
...
});
Second startup (given that we have a valid user.)
auto user = *app.get_current_user();
auto flx_sync_config = user.flexible_sync_configuration();
auto synced_realm = experimental::db(flx_sync_config);
synced_realm.write([&synced_realm]() {
...
});