I am quite new to Realm and now I'm facing the issue, mentioned in the title. Basically, one of my Realms
is a User
class (this is different from Users
that is created when new users log in) in Realm Cloud
. The class has a userId
property, which is set as its primary key. Now, I try to fetch users for a particular primary key with the following line:
let user = realm?.object(ofType: User.self, forPrimaryKey: "f677ca48b17bc7b90a886dd5d50148c1")
I do see that there is a user in the cloud with this primary key, but he is not always returned. I mean, say, if I run the same code 10 times, I will get the user only 4 times out of 10 (roughly). The above code is run from a method, which is always called when the controller is on screen. Also, there is no condition checking in the method for that part of code, so it is always called when the method is called.
I've also checked the realm
property and it is not nil
, so I don't quite understand why this happens.
The same happens when I try to get the current user by the line:
currentUser = realm?.object(ofType: User.self, forPrimaryKey: SyncUser.current?.identity)
The identity
value is not nil (I print it before calling the code).
UPDATED
Here is how the User
looks like:
class User: Object {
@objc dynamic var userId: String = ""
@objc dynamic var name: String = ""
override static func primaryKey() -> String? {
return "userId"
}
}
Maybe, I am missing something or that is a problem/bug on the cloud side, I don't know. If you've encountered such an issue and/or know what could cause it, I would greatly appreciate your help. Thanks in advance.