我有一個共享的單例類的NSObject的,我在運行某些操作隊列我得到這個崩潰:iOS - 如何從singleton NSObject中爲KVO移除觀察者? 。
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
看來,我需要使用「removeObserver:」爲防止這種情況的發生,但是我如何在共享對象上正確地做到這一點?
CODE:
-(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
你能後的崩潰日誌 – 2013-03-15 04:44:02
什麼代碼,在什麼類,崩潰?是什麼讓你相信去除觀察者會阻止它?不幸的是你的問題現在還不清楚。 – 2013-03-15 04:48:25
對不起,添加了代碼和崩潰日誌。謝謝您的幫助! – JimmyJammed 2013-03-15 04:50:21