同步后无法加载领域对象:尽管写入,领域还是空的
Trouble loading realm objects after sync: realm is empty despite writing
当我的程序注册用户时,它使用以下方法验证用户名和密码:
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])
}
我认为这足以让我在验证用户身份后从中创建领域 let realm = try! Realm()
和 write/read 是否正确?因为在写入领域后,注销用户 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 的一个实例...这意味着我正在查看另一个(离线)领域,而不是我进行写作的领域。在 authentication
函数中重新声明领域实例解决了问题。
当我的程序注册用户时,它使用以下方法验证用户名和密码:
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])
}
我认为这足以让我在验证用户身份后从中创建领域 let realm = try! Realm()
和 write/read 是否正确?因为在写入领域后,注销用户 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 的一个实例...这意味着我正在查看另一个(离线)领域,而不是我进行写作的领域。在 authentication
函数中重新声明领域实例解决了问题。