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?
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.