Realm Offline/Airplane mode


I’m working on a realm app. There are some tables in my realm object server. App basically fetch data from those tables and show up in list views in app. On launching, it authenticates realm admin user in background and create realm instance that app uses for CRUD operations.

My question is: Realm work offline which I know very well. Does it need internet connection on launching to get the user authenticated?

  1. On launching first time
  2. Let suppose, user launch the app, uses it for some time and then remove it from background and launch again from home screen of phone.

Please consider both situations in offline/airplane mode.

What I have tested it:-

User launched app(online) and get realm instance, then go offline, it works well. syncing does not get affected then.

but do we need internet connection to get realm admin authentication everytime?

Please suggest.

You need internet connection only the very first time a user has to login. After that, their identity and refresh tokens are persisted, so even if they subsequently launch the app while being offline, they won’t need to reauthenticate. Check out the Working with Users section of the docs for more information on User.Current and User.AllLoggedIn.

1 Like

Can someone explain the connection code that needs to be implemented as part of the app startup sequence so this works.

I would also like to know how to use User.AllLoggedIn to authenticate a user. From what I can see this will return a collection of User objects that have been previously authenticated however it only contains the Realm ID for the user to distinguish them.

Using simple authentication, if I have several users accessing the device and don’t log them out each time they finish using the app, and then get a previously logged in user to login again with a user name and password then, unless I have saved a list of users with their user name, password and Realm ID, I can’t verify if they are one of the logged in users.

If I do save the user name, password and Realm ID then I have to manage scenarios where a user password was reset through another app and hasn’t been reset on the current device.

Am I missing something that makes this all very simple?

If you’re online, you can always attempt a regular username/password login to authenticate the user. If you’re offline, then there would be no way for the device to know that the password was reset elsewhere, so authenticating with the old password should be fine. What you can do is, on successful authentication, you can save the username and the password hash in the keychain (or similar secure storage on other platforms), along with the user Id. Then, if the user is offline, you can ask them to enter their credentials, compare them to the value stored in the keychain, and if they match, grab the User instance from User.AllLoggedIn that matches the stored Id.