Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offline usage #120

Open
adamski opened this issue Nov 26, 2023 · 1 comment
Open

Offline usage #120

adamski opened this issue Nov 26, 2023 · 1 comment

Comments

@adamski
Copy link

adamski commented Nov 26, 2023

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?

@leemaguire
Copy link
Contributor

leemaguire commented Nov 29, 2023

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]() {
            ...
        });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants