Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Realm notifications

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.

like image 859
Dan DeLuca Avatar asked Jul 31 '15 19:07

Dan DeLuca


2 Answers

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'

like image 192
Idan Avatar answered Oct 05 '22 08:10

Idan


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()
    }
...
}
like image 35
Shmidt Avatar answered Oct 05 '22 07:10

Shmidt