How to use Realm notifications

10,537

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 variable

    var notificationToken: NotificationToken?
    
  • Set the notificationToken in viewDidLoad()

    notificationToken = realm.observe { [unowned self] note, realm in
       self.tableView.reloadData()
    }
    
  • Unregister from the Notification in viewWillDisappear(animated: Bool)

    notificationToken?.invalidate()
    

Edit notes:

  1. notificationToken.stop() is deprecated.
  2. realm.addNotificationBlock... will cause the following error:

    Value of type 'Realm' has no member 'addNotificationBlock'

Share:
10,537
Dan DeLuca
Author by

Dan DeLuca

Updated on June 15, 2022

Comments

  • Dan DeLuca
    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
    Amir Khorsandi over 7 years
    realm.removeNotificationdeprecated, use notificationToken.stop()
  • SergStav
    SergStav almost 5 years
    notificationToken.stop() is old, use notificationToken.invalidate()