- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个共享的 NSObject 单例类,我在其中运行了一些操作队列。我遇到了崩溃:
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
似乎我需要使用“removeObserver:”来防止这种情况发生,但我如何在共享对象上正确地做到这一点?
代码:
-(void)synchronizeToDevice{
queue = [NSOperationQueue new];
queue.name = @"SynchronizeToDeviceQueue";
//Sync Active User
NSInvocationOperation *operationUser = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(downloadUserData:)
object:[self activeUserID]];
[queue addOperation:operationUser];
//Sync Video Data
NSInvocationOperation *operationVideos = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(downloadVideoData)
object:nil];
[queue addOperation:operationVideos];
[queue addObserver:self forKeyPath:@"operations" options:0 context:NULL];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (object == queue && [keyPath isEqualToString:@"operations"]) {
//Synchronization Queue
if ([queue.name isEqualToString:@"SynchronizeToDeviceQueue"] && [queue.operations count] == 0) {
//Queue Completed
//Notify View Synchronization Completed
[self performSelectorOnMainThread:@selector(postNotificationDidFinishSynchronizationToDevice) withObject:nil waitUntilDone:NO];
}
//Video Download Queue
if ([queue.name isEqualToString:@"VideoFileDownloadQueue"] && [queue.operations count] == 0) {
//Notify View Video File Download Completed
[self performSelectorOnMainThread:@selector(postNotificationDidFinishDownloadingVideo) withObject:nil waitUntilDone:NO];
}
//Active User Sync Queue
if ([queue.name isEqualToString:@"SynchronizeActiveUserToDeviceQueue"] && [queue.operations count] == 0) {
//Queue Completed
//Notify View Synchronization Completed
[self performSelectorOnMainThread:@selector(postNotificationDidFinishActiveUserSynchronizationToDevice) withObject:nil waitUntilDone:NO];
}
}
else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
崩溃日志:
2013-03-14 21:48:42.167 COMPANY[1946:1103] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '<DataManager: 0x1c54a420>: An -observeValueForKeyPath:ofObject:change:context: message was received but not handled.
Key path: operations
Observed object: <NSOperationQueue: 0x1c5d3360>{name = 'SynchronizeActiveUserToDeviceQueue'}
Change: {
kind = 1;
}
Context: 0x0'
*** First throw call stack:
(0x336262a3 0x3b4b197f 0x336261c5 0x33f1a56d 0x21bd1 0x33eb46b9 0x33eb4313 0x33eb3a25 0x33eb3817 0x33f2b689 0x3b8ccb97 0x3b8cf139 0x3b8cd91d 0x3b8cdac1 0x3b8fda11 0x3b8fd8a4)
libc++abi.dylib: terminate called throwing an exception
最佳答案
我怀疑您对 synchronizeToDevice
的调用被调用了不止一次。如果是这样,您将继续观察旧队列以及一些新队列。当 observeValueForKeyPath:...
触发时,它可能会将旧队列传递给您,然后您忽略它,调用 super
,它会抛出异常,因为您没有处理您要求的观察。
你真正的问题是你没有使用访问器。那会让这更清楚。例如,这就是您将如何实现 setQueue:
-(void)setQueue:(NSOperationQueue *)queue {
if (_queue) {
[_queue removeObserver:self forKeyPath:@"operations"];
}
_queue = queue;
if (_queue) {
[_queue addObserver:self forKeyPath:@"operations" options:0 context:NULL];
}
}
现在,当您调用 self.queue = [NSOperationQueue new];
时,一切都会自动运行。您停止观察旧队列并开始观察新队列。如果您调用 self.queue = nil
,它会自动为您取消注册。
您仍然需要确保在 dealloc
中取消注册:
- (void)dealloc {
if (_queue) {
[_queue removeObserver:self forKeyPath:@"operations"];
}
}
关于iOS - 如何从 KVO 的单例 NSObject 中删除观察者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15424796/
我最近购买了《C 编程语言》并尝试了 Ex 1-8这是代码 #include #include #include /* * */ int main() { int nl,nt,nb;
早上好!我有一个变量“var”,可能为 0。我检查该变量是否为空,如果不是,我将该变量保存在 php session 中,然后调用另一个页面。在这个新页面中,我检查我创建的 session 是否为空,
我正在努力完成 Learn Python the Hard Way ex.25,但我无法理解某些事情。这是脚本: def break_words(stuff): """this functio
我是一名优秀的程序员,十分优秀!