Is removing a NotificationCenter observer that was created with closure syntax by name adequate?

你离开我真会死。 提交于 2019-12-21 03:53:14

问题


I have a few notifications that were created using block / trailing closure syntax which look like this:

NotificationCenter.default.addObserver(forName: .NSManagedObjectContextObjectsDidChange, object: moc, queue: nil) { note in
    // implementation
}

Which I was later removing by name, like this:

NotificationCenter.default.removeObserver(self, name: NSNotification.Name.NSManagedObjectContextObjectsDidChange, object: moc)

My Question

Is this adequate? Or do I absolutely need to save the NSObjectProtocol to it's own property and remove that property with the following syntax?

NotificationCenter.default.removeObserver(didChangeNotification)

回答1:


You absolutely need to store the return value in a property and remove that later on.

From https://developer.apple.com/reference/foundation/nsnotificationcenter/1411723-addobserverforname:

Return Value

An opaque object to act as the observer.

When you call any one of the removeObserver methods, the first parameter is the observer to remove. When you set up a block to respond to a notification, self is not the observer, NSNotificationCenter creates its own observer object behind the scenes and returns it to you.

Note: as of iOS 9, you are no longer required to call removeObserver from dealloc/deinit, as that will happen automatically when the observer goes away. So, if you're only targeting iOS 9, this may all just work, but if you're not retaining the returned observer at all, the notification could be removed before you expect it to be. Better safe than sorry.




回答2:


To add to @Dave's answer, it looks like documentation isn't always 100% accurate. According to this article by Ole Begemann there is a contradiction in the doc and self-removing magic was not happening as of iOS 11.2 in his test app.

So that the answer is still "Yes, one needs to remove that observer manually" (and yes, self is not the observer, the result of addObserver() method is the observer).




回答3:


Here an example with code, for how a correct implementation looks like:

Declare the variable that gets returned when you add the observer in your class A (the receiver of the notification or observer):

private var fetchTripsNotification: NSObjectProtocol?

In your init method add yourself as an observer:

init() {
    fetchTripsNotification = NotificationCenter.default.addObserver(forName: .needsToFetchTrips, object: nil, queue: nil) { [weak self] _ in
        guard let `self` = self else {
            return
        }
        self.fetchTrips()
    }
}

In the deinit method of your class, make sure to remove the observer:

deinit { NotificationCenter.default.removeObserver(fetchTripsNotification as Any) }

In your class B (the poster of the notification) trigger the notification like usually:

NotificationCenter.default.post(name: .needsToFetchTrips, object: nil)


来源:https://stackoverflow.com/questions/41924239/is-removing-a-notificationcenter-observer-that-was-created-with-closure-syntax-b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!