在Foundation中使用removeObserver:forKeyPath:崩溃 [英] Crash with removeObserver:forKeyPath: in Foundation
问题描述
我从Xcode的崩溃"部分检索到以下崩溃日志时遇到一些问题.此崩溃报告仅影响少数设备.
I having some problems with the following crash logs retrieved from the "Crashes" section in Xcode. Only few devices are affected by this crash report.
我已经分析了问题,但我认为这是Apple框架上的错误.但是我找不到复制它的方法.
I have analyzed the problem but I guess it's a bug on Apple framework. But I cannot find a way to replicate it.
这里有一个类似的讨论: removeObserver:forKeyPath中的崩溃帮助:
Here a similar discussion: Help with crash in removeObserver:forKeyPath:.
有任何提示吗?
线程0名称:线程0损坏:
Thread 0 name: Thread 0 Crashed:
0基金会
0x23507591 _NSKeyValueReplaceObservationInfoForObject + 69
(NSKeyValueObserving.m:1166)
0 Foundation
0x23507591 _NSKeyValueReplaceObservationInfoForObject + 69
(NSKeyValueObserving.m:1166)
1个基金会
0x23506fe7-[NSObject(NSKeyValueObserverRegistration)
_removeObserver:forProperty:] + 327(NSKeyValueObserving.m:1552)
1 Foundation
0x23506fe7 -[NSObject(NSKeyValueObserverRegistration)
_removeObserver:forProperty:] + 327 (NSKeyValueObserving.m:1552)
2基金会
0x23506b03-[NSObject(NSKeyValueObserverRegistration)removeObserver:forKeyPath:] + 163(NSKeyValueObserving.m:1696)
2 Foundation
0x23506b03 -[NSObject(NSKeyValueObserverRegistration) removeObserver:forKeyPath:] + 163 (NSKeyValueObserving.m:1696)
3基金会
0x235069a7-[NSObject(NSKeyValueObserverRegistration)
removeObserver:forKeyPath:context:] + 219(NSKeyValueObserving.m:1663)
3 Foundation
0x235069a7 -[NSObject(NSKeyValueObserverRegistration)
removeObserver:forKeyPath:context:] + 219 (NSKeyValueObserving.m:1663)
4 ApplicationName 0x0002e233-[主管removeObjectObserver:forKeyPath:] + 115(Supervisor.m:344)
4 ApplicationName 0x0002e233 -[Supervisor removeObjectObserver:forKeyPath:] + 115 (Supervisor.m:344)
其中removeObjectObserver:forKeyPath:
是
- (void) removeObjectObserver:(id)object forKeyPath:(NSString *)keyPath {
@try {
[object removeObserver:self forKeyPath:keyPath context:PrivateKVOContext];
} @catch (NSException *exception) { }
}
在使用Objective-C
中的
推荐答案
Observers
时必须格外注意:不要将相同的观察者时间乘以同一对象的属性,并包装移除内容如果有一个:
Observers
in Objective-C
must be used with extra attention: don't add the same observer multiples time to the same object's property, and wrap the removal if there is one :
if ([self observationInfo]) {
@try {
[self removeObserver:self forKeyPath:keyPath];
}
@catch (NSException *exception) {}
}
由于尝试删除两次观察者,或者正在移除不存在的观察者而导致崩溃.
You are experiencing crashes because you try to remove twice the observer, or you are removing a non-existant observer.
您应该这样添加observers
:
[yourObject addObserver:self forKeyPath:keypath options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionInitial context:nil/yourContext];
您可能会删除已经取消分配的对象上的观察者,从而导致崩溃.
You may remove an observer on an already deallocate object, resulting in this crash.
if (object && [self observationInfo]) {
@try {
[self removeObserver:self forKeyPath:keyPath];
}
@catch (NSException *exception) {}
}
这篇关于在Foundation中使用removeObserver:forKeyPath:崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!