Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

realm mobile platform, how to connect while offline?

the new realm mobile platform is advertised with offline support, however most tutorials does not show how that works in the examples...

for example, in their todo app example this is the code used to connect to the server database

SyncUser.logIn(with: .usernamePassword(username: username, password: password, register: false), server: URL(string: "http://127.0.0.1:9080")!) { user, error in
guard let user = user else {
    fatalError(String(describing: error))
}

DispatchQueue.main.async {
    // Open Realm
    let configuration = Realm.Configuration(
        syncConfiguration: SyncConfiguration(user: user, realmURL: URL(string: "realm://127.0.0.1:9080/~/realmtasks")!)
    )
    self.realm = try! Realm(configuration: configuration)

    // Show initial tasks
    func updateList() {
        if self.items.realm == nil, let list = self.realm.objects(TaskList.self).first {
            self.items = list.items
        }
        self.tableView.reloadData()
    }
    updateList()

    // Notify us when Realm changes
    self.notificationToken = self.realm.addNotificationBlock { _ in
        updateList()
    }
}
}

when the user goes offline, the returned user variable is nil, and you cant use the configured realm on the server, but the code doesn't show how to get the synced data from the mirrored local database... do you have to manually copy the items from the online database to a manually created local database everytime the user goes online?

like image 599
Hadi Avatar asked Jan 16 '17 08:01

Hadi


1 Answers

After you successfully log in (or register) the user it's cached in the device's keychain and you can retrieve it via currentUser property or alternatively if your app supports multiple users all even when you're offline.

Please note if you call logout on a user then it will be removed from the keychain and you will have to be online and log in again.

like image 143
Dmitry Avatar answered Oct 14 '22 00:10

Dmitry