gpt4 book ai didi

iOS - 如何从 KVO 的单例 NSObject 中删除观察者?

转载 作者:行者123 更新时间:2023-11-28 19:11:05 25 4
gpt4 key购买 nike

我有一个共享的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com