Swift4/Objective-C是否需要移除观察者?
Do observers need to be removed in Swift 4/Objective-C?
我正在尝试在 Swift 中实现 KVC/KVO 4. 我阅读的关于 KVC/KVO 在 Objective-C 中的大部分文档都指出观察者完成后需要将其删除。但是,在查看 Apple 使用 Swift 4 实现 KVO 的文档后,他们没有明确说明是否需要在 deinit 方法中删除观察者。它们在示例 class 定义中不包含 deinit 方法。但是,我不想做任何假设,因为我阅读的所有 Objective-C 文档都指出需要删除观察者。
我只是不确定是否需要通过 deinit 删除观察者。任何帮助或指向参考的方向都会很棒,谢谢。
从 Foundation Release Notes(强调)开始,10.13 放宽了关于注销观察员的规则:
Relaxed Key-Value Observing Unregistration Requirements
Prior to 10.13, KVO would throw an exception if any observers were still registered after an autonotifying object's -dealloc
finished running. Additionally, if all observers were removed, but some were removed from another thread during dealloc
, the exception would incorrectly still be thrown. This requirement has been relaxed in 10.13, subject to two conditions:
- The object must be using KVO autonotifying, rather than manually calling
-will
and -didChangeValueForKey:
(i.e. it should not return NO from +automaticallyNotifiesObserversForKey:
)
- The object must not override the (private) accessors for internal KVO state
If all of these are true, any remaining observers after -dealloc
returns will be cleaned up by KVO; this is also somewhat more efficient than repeatedly calling -removeObserver
methods.
HTH
如果你在谈论 NSKeyValueObservation
:不,他们没有。
来自 WWDC 2017 Video "What is new in Foundation"
的文字记录
There is no need for a deinit
where I throw away or tear down my observation because it's tied to the life cycle of that observation token. And so when the controller goes away, the observation token will go away.
我正在尝试在 Swift 中实现 KVC/KVO 4. 我阅读的关于 KVC/KVO 在 Objective-C 中的大部分文档都指出观察者完成后需要将其删除。但是,在查看 Apple 使用 Swift 4 实现 KVO 的文档后,他们没有明确说明是否需要在 deinit 方法中删除观察者。它们在示例 class 定义中不包含 deinit 方法。但是,我不想做任何假设,因为我阅读的所有 Objective-C 文档都指出需要删除观察者。
我只是不确定是否需要通过 deinit 删除观察者。任何帮助或指向参考的方向都会很棒,谢谢。
从 Foundation Release Notes(强调)开始,10.13 放宽了关于注销观察员的规则:
Relaxed Key-Value Observing Unregistration Requirements
Prior to 10.13, KVO would throw an exception if any observers were still registered after an autonotifying object's
-dealloc
finished running. Additionally, if all observers were removed, but some were removed from another thread duringdealloc
, the exception would incorrectly still be thrown. This requirement has been relaxed in 10.13, subject to two conditions:
- The object must be using KVO autonotifying, rather than manually calling
-will
and-didChangeValueForKey:
(i.e. it should not return NO from+automaticallyNotifiesObserversForKey:
)- The object must not override the (private) accessors for internal KVO state
If all of these are true, any remaining observers after
-dealloc
returns will be cleaned up by KVO; this is also somewhat more efficient than repeatedly calling-removeObserver
methods.
HTH
如果你在谈论 NSKeyValueObservation
:不,他们没有。
来自 WWDC 2017 Video "What is new in Foundation"
的文字记录There is no need for a
deinit
where I throw away or tear down my observation because it's tied to the life cycle of that observation token. And so when the controller goes away, the observation token will go away.