User partition not reading data

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._partition = partition
        self.uid = uid = 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:
                    if results.count > 0 {
               = results[0].name
                case .update(let results, _, _, _):
                    if results.count > 0 {
               = results[0].name
                case .error(let error):
                    // An error occurred while opening the Realm file on the background worker thread

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?


@Richard_Krueger How are you opening the Realm? Are you making sure to use realm.asyncOpen? This will download the data to disk first before returning a valid realm reference for you to observe

@Ian_Ward This is what I was using

self.userRealm = 
   try! Realm(configuration: user.configuration(partitionValue: uid))

I was not using realm.asyncOpen.

@Ian_Ward Ok I found the section describing this issue in the docs

Quick question, we are also writing a Node.js app as well. Is there an equivalent to realm.asyncOpen in Node.js?

Yes. For node.js you can use

@Ian_Ward We have switched to using realm.asyncOpen and the problem seems to have gone away. Thanks for the tip.

I would suggest getting the docs folks to update the Quick Start section on “Open A Realm” to reflect this.

Thank you guys !!
I was beating my head having the same issue asyncOpen did it.

@Barry_Fawthrop your welcome, I still have a head wound, but my program runs great!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.