Realm Swift how to add observer to object type (nil)

Nik Kov

Assume i have two flows:

1) I have data in database, then i use fetch entity with params from database and set and observer. After that i load data from server and observer's block fires successfully. That's fine.

2) I don't have data in database. Then i try to do the same, it looks like:

    myObject = MyRealmService()
        .fetchAll(MyRealmObject.self,
                  filter: "userID == \(someID)")?
        .first
    realmToken = myObject?.observe { [weak self] change in
        guard let _self = self else { return }

        switch change {
        case .deleted:
            _self.popCurrentViewController()
        case .error(let error):
            _self.show(error: error)
        case .change:
            _self.updateUI()
        }
    }

    loadDataFromServer() { object in
         object.saveToRealm()
    }

Then myObject is nil, so the notification block don't setting.

What is the way to handle notifications in this way? I mean, maybe somehow we can set the block to the filter type (MyRealmObject.self, filter: "userID == \(someID)"), so if in realm has write the object that fits it, then the observe block fires?

Sandeep

Instead of observing individual object which is still not stored in Realm database, you could observe Realm Results

   let realm = try! Realm()

    var results = realm.objects(MyRealmObject.self).filter: "userID == \(someID)")

    var notificationToken = results.observe { change in
        switch change {
        case .update:
            DispatchQueue.main.async {
                block()
            }
        default: ()
        }
    }

You can observe the result object instead of individual and do some action based on changes.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Realm Object returning nil (Swift)

How can I add or update a Realm object in Swift?

Realm: How to add two Results<(Object)> in Realm

How to Nil a object in Swift

How do I resolve the generic for Realm's object observer?

How to delete object in realm in Swift

how to update an object in realm swift

How to add observer for view frame change in swift

How to check object is nil or not in swift?

How to add a nested List of objects in Realm "Error: JS value must be of type: object"

How to add Realm Object class into RLMArray

How to add new property to a Realm Object?

How to model a Swift dictionary property on a Realm object?

How to filter object from Realm database in Swift

How to add an observer in Objective-C and fire a Notification in Swift 3

Realm Object fields are nil sometimes

Updating Realm object with Nil not working

How to add nil value to a NSArray in swift?

How to add nil value to Swift Dictionary?

Swift Optional type: how .None == nil works

Add Observer and Selector in Swift 3

How to retrieve an object that is in a list in another object using realm swift?

error: Type 'int[]' of field is not supported | how to add integer list to Realm?

How can I to add object to the List<Foo> via Realm Browser

How do I add a subclass to a List<superclass> in Realm Swift

How can I filter by specific date from realm object in swift?

How can I convert a Realm object to JSON in Swift?

realm swift . How to link to exist object with primary key

how to define the type of an observer in typescript

TOP Ranking

HotTag

Archive