领域移动平台,离线时如何连接? [英] realm mobile platform, how to connect while offline?

查看:40
本文介绍了领域移动平台,离线时如何连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新的领域移动平台在广告中提供了离线支持,但是大多数教程并未在示例中展示它的工作原理...

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

例如,在他们的 todo应用示例中,这是用于连接到服务器数据库

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()
    }
}
}

当用户脱机时,返回的user变量为nil,并且您无法在服务器上使用配置的领域,但是代码未显示如何从镜像的本地数据库中获取同步的数据...每当用户上线时,您是否必须将项目从在线数据库手动复制到手动创建的本地数据库?

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.

请注意,如果您在某个用户上呼叫logout,它将被从钥匙串中删除,您将必须在线并再次登录.

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.

这篇关于领域移动平台,离线时如何连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆