Search code examples
swiftrealmrealm-mobile-platformrealm-object-server

Realm Object Server Swift, changing default permission of a realm


I created a realm with an admin account and when i look at my dashboard my realm is there. Its owner column is blank though? Is it normal? Because I opened that synced realm with my admin account.

My main question is this, in default permissions it says "no access". I tried to give all users permission to write in that realm shown in below:

SyncUser.logIn(with: admin, server: serverURL) { (user, error) in

        let permission = SyncPermissionValue(realmPath: "realm://myServerIp/swipeItApp/", username: "*", accessLevel: .write)

        user?.applyPermission(permission, callback: { (error) in

            if error != nil {

                print(error?.localizedDescription)

            } else {

                print("success")

            }

        })

    }

but neither error or success prints. What is wrong in my code? Thanks!


Solution

  • Thanks for using Realm.

    The reason you are not seeing the callback is because you are running the applyPermission() method directly in the logIn() callback. The applyPermission() method has to be run on a thread with an active run loop, and the logIn() callback runs on threads without run loops managed by a background queue.

    To fix this problem, use DispatchQueue.main.async to dispatch your code back onto the main queue:

    SyncUser.logIn(with: admin, server: serverURL) { (user, error) in
        let permission = SyncPermissionValue(realmPath: "realm://myServerIp/swipeItApp/", username: "*", accessLevel: .write)
        DispatchQueue.main.async {
            user?.applyPermission(permission, callback: { (error) in
                if error != nil {
                    print(error?.localizedDescription)
                } else {
                    print("success")
                }
            })
        }
    }
    

    We consider the need to use DispatchQueue.main.async for this use case a design mistake and will be changing the way logIn() works in our upcoming Realm 3.0.0 release to run the callback on the main queue by default. So if you upgrade to Realm 3.0.0 once it's released you will no longer need to use the workaround I detailed above; your original code should work as-is.