Is realm offline login really supported?

Hello, I’m working on an Electron app with Realm sync enabled and I’m confused with the doc, especially this paragraph Open a Synced Realm While Offline
Initially I thought that you could log in and out of your app while Offline if the very first time you logged in with an Internet connection. It was my understanding that mongodb Realm did some magic caching of the credentials and allowed you to do logins checks while offline the subsequent times.
This was also influenced by the orange important box saying " Offline Login is Supported for Both Flexible and Partition-Based Sync Configurations".
Now that I try to implement this and that it doesn’t work I’m not so sure anymore of my first understanding.
Now I’m starting to believe that the user has to be already logged in, you cannot actually perform a user.logIn while offline. Am I correct?

Hi @Benoit_Werner,

Yes, of course, the authentication process requires a connection to proceed.

Indeed it does: unless you, in your code, execute an explicit logOut(), the app.currentUser property will still be set, and you can proceed to work offline.

In practice, a typical login workflow looks like

    let user = app.currentUser;

    if (!user || !user.isLoggedIn) {
      // No current user, log them in
      let credentials;

      // … set credentials, according to your app setup…

      user = await app.logIn(credentials);

      console.log(`Logged in with the user: ${user.id}`);
    } else {
      console.log(`Skipped login with the user: ${user.id}`);
    }
    // …proceed to open the realm with the set user
1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.

How frequently does the bug occur?

Always

Description

Im using Java Realm SDK.
The app works fine when network connectivity is available. App even works when i turn off data connectivity after opening app.
But when i restart app with no data connectivity. It is unable to fetch the local data.

How to Reproduce:
Turn off data connectivity.
Open app
App unable to sync.

What I got after diagnosing:

After I restart the app without data connectivity. The app is unable to set SyncConfiguration as a result realm is not able to fetch any data.
Hence as Mongo says it is offline first I’m able to use this important feature.

But when I turn off data while running app. the app works fine. as SyncConfiguration is already set.

This is My sync configuration:

config = new SyncConfiguration.Builder(app.currentUser())
                    .allowWritesOnUiThread(true)
                    .allowQueriesOnUiThread(true)
                    .compactOnLaunch()
                    .waitForInitialRemoteData(500, TimeUnit.MILLISECONDS)
                    .initialSubscriptions(new SyncConfiguration.InitialFlexibleSyncSubscriptions() {
                        @Override
                        public void configure(Realm realm, MutableSubscriptionSet subscriptions) {
                            // add a subscription with a name
                            Log.e(TAG, "configure: ");

                            Subscription userSubscription = subscriptions.find(kMap.userSubs);
                            if (userSubscription == null) {
                                subscriptions.addOrUpdate(Subscription.create(kMap.userSubs,
                                        realm.where(users.class)));
                            }

                            Subscription collegeSubscription = subscriptions.find(kMap.collegeSubs);
                            if (collegeSubscription == null) {
                                subscriptions.addOrUpdate(Subscription.create(kMap.collegeSubs,
                                        realm.where(colleges.class)));
                            }

                            Subscription courseSubscription = subscriptions.find(kMap.courseSubs);

                            if (courseSubscription == null) {
                                subscriptions.addOrUpdate(Subscription.create(kMap.courseSubs,
                                        realm.where(courses.class)));
                            }

                            Subscription studentsSubs = subscriptions.find(kMap.studentsSubs);
                            if (studentsSubs == null) {
                                subscriptions.addOrUpdate(Subscription.create(kMap.studentsSubs,
                                        realm.where(students.class)));
                            }


                            realm.close();
                        }
                    })
                    .build();

            Realm.setDefaultConfiguration(config);
            StaticValues.syncCount++;

Error I get while internet is off:

E/REALM_JAVA: Session Error[wss://realm.mongodb.com/]: UNKNOWN(realm.util.network.resolve:1): Host not found (authoritative)
E/REALM_SYNC: Failed to resolve ‘ws.ap-south-1.aws.realm.mongodb.com:443’: Host not found (authoritative)

G’Day @Clink_App ,

Thank you for raising your concern.

Your question seemed similar to this post that has been answered. I presume when you restart the app, you remove the cache data that is saved and you would need internet connectivity to login back again so the data can be synced from the server.

I hope this helps answer your question or let me know if I mistook a co-relation here.

Cheers, :performing_arts:
henna