I want to observe the object count of the realm DB. I wrote this approach:
let realm = try? Realm()
self.notificationToken = realm?.objects(AnalyticsEventDto.self).observe { _ in
if let count = realm?.objects(AnalyticsEventDto.self).count {
observer.send(value: count)
}
}
There is another way to do so? Thanks
I would use the
RealmCollectionChangeparameter rather than creating a newResultsobject in the closure. This also lets you handle the error:But a more ReactiveSwifty way of doing this might be to wrap the entire Realm collection notification API to make it reactive. This is a bit involved, but it is more general and potentially usable in multiple places in your code:
You could then use it like this: