简体   繁体   English

同步后无法加载领域对象:尽管编写,领域仍然为空

[英]Trouble loading realm objects after sync: realm is empty despite writing

When my program signs up a user, it authenticates the username and password using the following method: 当我的程序注册用户时,它使用以下方法对用户名和密码进行身份验证:

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

        }
    }
}

The setDefaultRealmConfiguration(with: user) looks as follows: 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])
}

Am I correct to assume that this is enough for me to create a realm let realm = try! Realm() 我是否正确地假设这足以创建一个领域? let realm = try! Realm() let realm = try! Realm() and write/read from it after authenticating a user? 验证用户身份后, let realm = try! Realm()并从中进行写入/读取? Because after writing to the realm, logging out a user SyncUser.current.logOut() , then signing back in using authenticate , all written information is seemingly lost. 因为在写入领域之后,注销用户SyncUser.current.logOut() ,然后使用authenticate ,所有写入的信息似乎都丢失了。 The information is not much, just a few strings (nevertheless, I implemented a timer to see if that worked - it didn't). 信息不是很多,只有几个字符串(尽管如此,我实现了一个计时器来查看它是否有效-无效)。

Here are the logs: 以下是日志:

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

Turns out, I was initializing an instance of Realm before doing the default realm configuration... this meant I was looking into another (offline) realm rather than the one where I did the writing. 事实证明,在进行默认领域配置之前,我正在初始化Realm的实例……这意味着我正在研究另一个(脱机)领域,而不是进行编写的领域。 Redeclaring the realm instance inside the authentication function fixed the problem. authentication功能内重新声明领域实例可以解决该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM