我正在使用以下函数在 nsoperationqueue 中的操作完成后通知我的应用程序,以便我可以安排取决于操作结果的任务。我正在使用:
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if([keyPath isEqual:@"isFinished"] && _operation == object)
{
NSLog(@"Our Thread Finished!");
[_operation removeObserver:self forKeyPath:@"isFinished"];
[self performSelectorOnMainThread:@selector(showDialog) withObject:nil waitUntilDone:YES];
}
}
我的问题是,由于分配给这些操作的任务大多是解析数据,如果我尝试点击其他按钮或基本上做一些导致动作的事情,我会得到以下异常:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '<Settings: 0x21b970>: An -observeValueForKeyPath:ofObject:change:context: message was received but not handled.
Key path: isFinished
我完全理解,因为我尝试在主线程上做其他事情,因此对主线程的调用:
[self performSelectorOnMainThread:@selector(showDialog) withObject:nil waitUntilDone:YES];
未能被执行。但是这个问题的解决方案是什么,因为我希望既允许用户在发出请求后执行任何操作,又在完成分配给操作的任务后执行计划的操作。
真的有可能吗?
提前谢谢。