0

I have used the techniques in the RealmTask tutorial (https://realm.io/docs/tutorials/realmtasks/ ) to get a demonstration of synchronisation with the Realm Object Server working. However, as mentioned in realm mobile platform, how to connect while offline? , it is difficult to find design guidelines on realising a robust app in the presence of intermittent network connectivity. For example, the network might not be available when the app is first run, and in the tutorial example I think the login attempt would just time out after say 30 seconds.

From various sources, I have tried to outline an implementation approach on the client and have come up with the following:

=============================================================

At start-up of app

Create login credentials with

SyncCredentials.usernamePassword()

Check whether user credentials already exist using

SyncUser.all

If so, get the correct user using the appropriate key (UserId)

If a user is obtained, get the Realm configuration using

realmConfiguration = Realm.Configuration(SyncConfiguration(user, realmURL))

Attempt a log-in with

SyncUser.logIn with SyncCredentials

On completion, put the following on the main DispatchQueue (async)

realmConfiguration = Realm.Configuration(SyncConfiguration(user, realmURL))

if not logged in, repeat login attempts every N minutes until successful? E.g. to handle the situation when the network is unavailable when the app is started, but then becomes available?

Launch the rest of the app, making realmConfiguration available.

However, only access the Realm if realmConfiguration has been set up. Design the app so that it handles the scenario of realmConfiguration not being set up.

=============================================================

Is the above approach sensible, or is there a better solution?

gimbal
  • 63
  • 8

1 Answers1

0

Katsumi from Realm here. Our RealmTasks demo application may help you. https://github.com/realm-demos/realm-tasks/tree/master/RealmTasks%20Apple

First, check whether the user has logged in or not at launched the app.

if configureDefaultRealm() {
    window?.rootViewController = ContainerViewController()
    window?.makeKeyAndVisible()
} else {
    window?.rootViewController = UIViewController()
    window?.makeKeyAndVisible()
    logIn(animated: false)
}

https://github.com/realm-demos/realm-tasks/blob/master/RealmTasks%20Apple/RealmTasks%20iOS/AppDelegate.swift#L35

If the user has been logged in before, you can use user object that was cached before. (SyncUser.current or SyncUser.all)

If there is no cached user object (The user is the first time to use the app, or the user re-installs the app), show login view to signing up/in.

The former case (Use the cached user object) doesn't require network access, so you don't need to care about the offline situation.

The latter case (The user should signing up/in) requires network access, in that case, the best practice depends on the specification of the app. It is enough to show a just alert view that indicates requiring network for some apps, or use standalone Realm and then migrate synced realm after the app will be online.

kishikawa katsumi
  • 10,418
  • 1
  • 41
  • 53