“Operation canceled” Realm Error Domain=io.realm.unknown Code=89

[亡魂溺海] 提交于 2020-08-10 20:20:21

问题


I get the exact same error as #62999928, but not with the same configuration. I'm just trying to access a global Realm file which is on my Realm Cloud (I am NOT on MongoDB and the beta).

I get the “Operation canceled” Realm Error Domain=io.realm.unknown Code=89 error when I try to open the Realm.

Realm Studio:

Opening code:

let url = URL(string: "realms://\(MY_INSTANCE_ADDRESS)/common")!
let config = user.configuration(realmURL: url, fullSynchronization: true)

Realm.asyncOpen(configuration: config) { ... }

Authentication code (using PromiseKit):

private func connectToRealm(_ firebaseUser: User) -> Promise<SyncUser> {
    
    // if realm user already logged in, check if it's the right user
    if let user = SyncUser.current {
        guard user.identity == firebaseUser.uid else {
            // it's not the right user. log out and try again.
            user.logOut()
            return connectToRealm(firebaseUser)
        }
        
        // it's the right user.
        return Promise.value(user)
    }

    return firstly {
        // get JWT token with firebase function
        Promise {
            Functions.functions().httpsCallable("myAuthFunction").call(completion: $0.resolve)
        }
        
    }
    .compactMap { result in
        // extract JWT token from response
        return (result?.data as? [String: Any])?["token"] as? String
        
    }
    .then { token in
        // connect to Realm
        Promise {
            SyncUser.logIn(with: SyncCredentials.jwt(token), server:MY_AUTH_URL, onCompletion: $0.resolve)
        }
    }
}

This method is called after logging with Firebase. The auth URL is just https://\(MY_INSTANCE_ADDRESS).

I suspect it's a permission problem, in which case: how can I easily let this file be readable by everyone, but not writable? I'm not planning on creating a lot of theses files, I just want to do this once.

EDIT: I reduced my code to the bare minimum.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
    let MY_INSTANCE_ADDRESS = [...]
    let AUTH_URL = URL(string: "https://\(MY_INSTANCE_ADDRESS)")!
    let COMMON_REALM_URL = URL(string: "realms://\(MY_INSTANCE_ADDRESS)/common")!

    SyncUser.logIn(with: SyncCredentials.anonymous(), server: AUTH_URL) { (user, error) in
        guard error == nil && user != nil else {
            print(error)
            return
        }
        
        let config = user!.configuration(realmURL: COMMON_REALM_URL, fullSynchronization: true)
        Realm.asyncOpen(configuration: config) { (realm, error) in
            guard error == nil && realm != nil else {
                print(error)
                return
            }

            print("Successfully loaded Realm file with \(realm!.objects(MyObjectType.self).count) objects.")
            
        }
    }
}

Nothing else is running, I don't load my app window. Still getting the same error.

EDIT 2: changed code according to @Jay's comment.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
    let MY_INSTANCE_ADDRESS = [...]
    let AUTH_URL = URL(string: "https://\(MY_INSTANCE_ADDRESS)")!
    let COMMON_REALM_URL = URL(string: "realms://\(MY_INSTANCE_ADDRESS)/common")!

    SyncUser.logIn(with: SyncCredentials.anonymous(), server: AUTH_URL) { (user, error) in
        guard error == nil && user != nil else {
            print(error)
            return
        }

        print("logged in")
        
        let config = SyncUser.current?.configuration(realmURL: COMMON_REALM_URL, fullSynchronization: true)
        
        let realm = try! Realm(configuration: config!)
        
        // $$$
        let objectsResult = realm.objects(MyObjectType.self)
        self.notificationToken = objectsResult.observe { changes in
            print("changes: \(changes)")

            if case .initial(let results) = changes {
                print("\(results.count) results: \(results)")
            }

        }
        // $$$
    }
}

Result:

logged in
changes: initial(Results<MyObjectType> <0x155d2d4e0> (

))
0 results: Results<MyObjectType> <0x155d2d4e0> (

)

No error, but I don't get the content that are on the cloud.

EDIT 3 Okay that's interesting. By changing the code between the $$$ to:

        try! realm.write {
            realm.create(MyObjectType.self, value: MyObjectType(id: 2021, name_fr: "test_fr", name_eng: "test_eng"))
        }
        
        let objectsResult = realm.objects(MyObjectType.self)
        print(objectsResult)

I get the result:

logged in
Results<MyObjectType> <0x143fe0510> (
    [0] MyObjectType {
        id = 2020;
        name_fr = test_fr;
        name_eng = test_eng;
    }
)

So the object is being written. When I run again the same code (without the login part) and changing the object id, I get:

Results<MyObjectType> <0x111d507c0> (
    [0] MyObjectType {
        id = 2020;
        name_fr = test_fr;
        name_eng = test_eng;
    },
    [1] MyObjectType {
        id = 2021;
        name_fr = test_fr;
        name_eng = test_eng;
    }
)

So the objects are stored somewhere. However, I don't see these two objects in Realm Studio. In Realm Studio, I see the objects that I added using a JS script, which should be downloaded to the device but are not.

When I run again the code after logging out, the objects are removed. All the URLs are correct, and I checked that no other Realm file is created by running the code.

来源:https://stackoverflow.com/questions/63014699/operation-canceled-realm-error-domain-io-realm-unknown-code-89

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!