rodrigochousal
rodrigochousal

Reputation: 401

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:

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() and write/read from it after authenticating a user? 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. 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

Upvotes: 0

Views: 323

Answers (1)

rodrigochousal
rodrigochousal

Reputation: 401

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. Redeclaring the realm instance inside the authentication function fixed the problem.

Upvotes: 1

Related Questions