当我的程序注册用户时,它使用以下方法验证用户名和密码:
func authenticate(username: String, password: String, register: Bool, callback: @escaping (NSError?) -> Void) {
let credentials = SyncCredentials.usernamePassword(username: username, password: password, register: register)
SyncUser.logIn(with: credentials, server: Constants.syncAuthURL) { user, error in
DispatchQueue.main.async {
if let user = user {
setDefaultRealmConfiguration(with: user)
}
// handle error
}
}
}
setDefaultRealmConfiguration(with: user)
如下所示:
public func setDefaultRealmConfiguration(with user: SyncUser) {
print("DEFAULT REALM CONFIG SET")
Realm.Configuration.defaultConfiguration = Realm.Configuration(
syncConfiguration: SyncConfiguration(user: user, realmURL: Constants.syncServerURL!),
objectTypes: [UserData.self, Achievement.self])
}
我是否认为这足以让我创建一个 Realm let realm = try! Realm()
并在对用户进行身份验证后写入/读取?因为在写入 Realm 后,注销用户 SyncUser.current.logOut()
,然后使用 authenticate
重新登录,所有写入的信息似乎都丢失了。信息不多,只有几个字符串(尽管如此,我实现了一个计时器来查看它是否有效 - 它没有)。
这是日志:
DEFAULT REALM CONFIG SET
WRITING DATA...
LOGGED OUT
DEFAULT REALM CONFIG SET
2017-03-12 18:11:14.133 appName[21003:711586] Sync: Opening Realm file:
/Users/name/Library/Developer/CoreSimulator/Devices/09F772ED-0E1C-4836-
913F-8541CBF31B23/data/Containers/Data/Application/B6E37EF3-9992-4682-
9F44-3828148A2DC6/Documents/realm-object-server/14d9448af666e6d6a68a017a301508e4/realm%3A%2F%2FIP%3A9080%2F%7E%2FuserRealm
2017-03-12 18:11:14.292 appName[21003:711586] Sync: Connection[1]: Session[1]: Starting session for '/Users/name/Library/Developer/CoreSimulator/Devices/09F772ED-0E1C-4836-913F-8541CBF31B23/data/Containers/Data/Application/B6E37EF3-9992-4682-9F44-3828148A2DC6/Documents/realm-object-server/14d9448af666e6d6a68a017a301508e4/realm%3A%2F%2FIP%3A9080%2F%7E%2FuserRealm'
2017-03-12 18:11:14.292 appName[21003:711586] Sync: Connection[1]: Resolving 'IP:9080'
2017-03-12 18:11:14.293 appName[21003:711586] Sync: Connection[1]: Connecting to endpoint 'IP:9080' (1/1)
2017-03-12 18:11:14.334 appName[21003:711586] Sync: Connection[1]: Connected to endpoint 'IP:9080' (from '192.168.1.78:59882')
2017-03-12 18:11:14.392 appName[21003:711586] Sync: Connection[1]: Session[1]: Sending: BIND(server_path='/14d9448af666e6d6a68a017a301508e4/userRealm', signed_user_token_size=605, need_file_ident_pair=1)
2017-03-12 18:11:14.444 appName[21003:711586] Sync: Connection[1]: Session[1]: Received: ALLOC(server_file_ident=930134346122185885, client_file_ident=1, client_file_ident_secret=1026730466566833306)
2017-03-12 18:11:14.451 appName[21003:711586] Sync: Connection[1]: Session[1]: Sending: IDENT(server_file_ident=930134346122185885, client_file_ident=1, client_file_ident_secret=1026730466566833306, scan_server_version=0, scan_client_version=0, latest_server_version=0, latest_server_session_ident=0)
REALM IS EMPTY
最佳答案
事实证明,在进行默认 Realm 配置之前,我正在初始化一个 Realm 实例……这意味着我正在查看另一个(离线) Realm ,而不是我进行写作的 Realm 。在 authentication
函数中重新声明 Realm 实例解决了这个问题。
关于swift - 同步后无法加载 Realm 对象 : realm is empty despite writing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42755022/