We have this random bug that is occurring on reading data from a user partition. We are writing a simple Swift app that syncs. Our table is called UserData
class UserData: Object {
@objc dynamic var _id = ObjectId.generate()
@objc dynamic var _partition = ""
@objc dynamic var uid = ""
@objc dynamic var name = ""
override static func primaryKey() -> String? {
return "_id"
}
override static func indexedProperties() -> [String] {
return ["uid"]
}
convenience init(uid: String, partition: String, name: String) {
self.init()
self._partition = partition
self.uid = uid
self.name = name
}
}
The partition is always set to the uid. Upon signup we create a user data for each user. That works fine. It’s later on when we try to read it, we have problems.
let results = RealmManager.shared.userRealm.objects(UserData.self)
self.notificationToken = results.observe { (changes: RealmCollectionChange) in
switch changes {
case .initial:
NSLog("initial")
if results.count > 0 {
self.name = results[0].name
}
case .update(let results, _, _, _):
NSLog("update")
if results.count > 0 {
self.name = results[0].name
}
case .error(let error):
// An error occurred while opening the Realm file on the background worker thread
fatalError("\(error)")
}
}
This code works most of the time, but for some users we can never retrieve the record. We see the record in Compass, but always get a result count of zero back in code. If we terminate the “Sync” - not pause it - the bug goes away. This seems completely random. I was wondering if we need to make the partition key indexed?
Thanks