How to use Realm notifications
Solution 1
Make notificationToken
an ivar:
var notificationToken: NotificationToken?
deinit{
//In latest Realm versions you just need to use this one-liner
notificationToken?.stop()
/* Previously, it was needed to do this way
let realm = Realm()
if let notificationToken = notificationToken{
realm.removeNotification(notificationToken)
}
*/
}
override func viewDidLoad() {
super.viewDidLoad()
let realm = Realm()
notificationToken = realm.addNotificationBlock { [unowned self] note, realm in
self.tableView.reloadData()
}
...
}
Solution 2
From Realm latest docs (3.0.1):
Add notificationToken.invalidate()
in order to unregister from the notification.
In Detail:
-
Declare the
notificationToken
as class variablevar notificationToken: NotificationToken?
-
Set the
notificationToken
inviewDidLoad()
notificationToken = realm.observe { [unowned self] note, realm in self.tableView.reloadData() }
-
Unregister from the Notification in
viewWillDisappear(animated: Bool)
notificationToken?.invalidate()
Edit notes:
-
notificationToken.stop()
is deprecated. -
realm.addNotificationBlock...
will cause the following error:Value of type 'Realm' has no member 'addNotificationBlock'
Dan DeLuca
Updated on June 15, 2022Comments
-
Dan DeLuca almost 2 years
I am trying to write an application in OS X using a Realm database. In my program I need to wait for a Realm write to be complete then call up a new veiwcontroller. After much research it seems that using Realm's built in notification center would be appropriate. According to the Realm documentation the format should work like this
let token = realm.addNotificationBlock { notification, realm in viewController.updateUI() }
I understand that this is a swift closure, but I am not sure how to use it. If I changed the code to this
let token = realm.addNotificationBlock { notification, realm in println("The realm is complete") }
Would that print to my debugging screen when the write is complete? Or more simply how do I execute some code only after I receive the notification?
If I place the above code in my app I do not see my line in the debug screen all I see is the following:
2015-07-31 16:08:17.138 Therapy Invoice[27979:2208171] RLMNotificationToken released without unregistering a notification. You must hold on to the RLMNotificationToken returned from addNotificationBlock and call removeNotification: when you no longer wish to receive RLMRealm notifications.
-
Amir Khorsandi over 7 years
-
SergStav almost 5 yearsnotificationToken.stop() is old, use notificationToken.invalidate()