Does the observer NotificationCenter that was created with closing syntax by name?

I have several notifications that were created using the block / trailing closure syntax that look like this:

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

What I later deleted by name, for example:

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

My question

Is this adequate? Or do I absolutely need to save NSObjectProtocolits own property and delete this property with the following syntax?

NotificationCenter.default.removeObserver(didChangeNotification)
+4
source share
1 answer

You absolutely must save the return value in the property and delete it later.

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

.

- removeObserver, - . , self , NSNotificationCenter .

: iOS 9 removeObserver dealloc/deinit, , . , iOS 9, , , , . .

+5

Source: https://habr.com/ru/post/1668196/


All Articles