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.
答案 0 :(得分:13)
来自Realm latest docs(3.0.1):
添加notificationToken.invalidate()
以取消注册通知。
详细信息:
将notificationToken
声明为类变量
var notificationToken: NotificationToken?
在notificationToken
viewDidLoad()
notificationToken = realm.observe { [unowned self] note, realm in
self.tableView.reloadData()
}
从viewWillDisappear(animated: Bool)
notificationToken?.invalidate()
编辑笔记:
notificationToken.stop()
已被弃用。realm.addNotificationBlock...
会导致以下错误:
类型' Realm'没有会员' addNotificationBlock'
答案 1 :(得分:6)
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()
}
...
}