RealmSwift.changesetPublisher(object)

Hi,

i am working on a little app with Realm and trying to figure out, why there is no notification for deletion, when observing a single object with a Publisher.

     func subscribe(primaryKey key: String, completion: @escaping ((Result<LocationModel, LocationError>) -> Void)) {
            
            guard let location: DBLocationModel = self.realmService.fetch(id: key) else {
                return completion(.failure(.noLocation))
            }
            
            self.subscribeBy = RealmSwift.changesetPublisher(location)
                .receive(on: DispatchQueue.main)
                .sink(receiveValue: { response in
                    switch response {
                    
                    case .change(let location, _):  completion(.success(self.locationWrapper.map(location)))
                        
                    case .deleted:                print("go back to rootview, but nothing is happening :(")
                        
                    case .error:                    completion(.failure(.noLocation))
                    }
                })
                
        }

if i am changing the object, it is working well and updating. but if i am deleting this object out of the database, there is no action.

anyone got the same thing and figured out why?

Hi @Alexander_Puchta ,
As soon as you delete this object all subs will be invalidated.
Instead you could subscribe to Results<DBLocationModel>

        var results = realm.objects(DBLocationModel.self) // Results remains valid even if you delete all objects
        var subscribeBy = results.changesetPublisher
            .receive(on: DispatchQueue.main)
            .sink(receiveValue: { response in
                print(response)
            })

Yes i subscribe to complete db at the main screen, but this is my detail screen and my app will be able to get edited with iphone and watch at the same time. so i am trying to get this notifiation on detail screen, so i can pop back to main view, without notifications. there has to be a solution within combine, and i am trying to figure this out :slight_smile: but thanks for your advice.

Object publishers (both changeset and non) report that the object has been deleted by completing the pipeline rather than sending a .deleted message. This can be handled with the receiveCompletion: callback on sink(), or if you’d prefer to get a .deleted message you can add .append(.deleted) before .sink().

It turns out we forgot to actually document this anywhere.

1 Like

With Combine you can use it in little bit another way.

        self. subscribeBy = valuePublisher(location)
            .receive(on: DispatchQueue.main)
            .sink(receiveCompletion: { result in
                print(result)
            }, receiveValue: { value in
                print(value)
            })
1 Like

Thank you guys, i will test this tomorrow. stay safe!

Well, great. This is my solution. Thanks a lot!

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