gpt4 book ai didi

ios - observeValueForKeyPath 没有被调用

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:41:16 31 4
gpt4 key购买 nike

我正在开发一个测试应用程序,其中有一个 NSOperationQueue。我正在创建一个 NSInvocationOperation 并观察该操作的“isFinished”属性。奇怪的是,observeValueForKeyPath 仅有时被调用。我无法理解每次都调用它必须进行的更改。请帮忙。

这是我写的代码:

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
........//initialization

queue = [NSOperationQueue new];
operation=[NSInvocationOperation new];

operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(CreateOperationWithContext:) object:context];

[operation addObserver:self forKeyPath:@"isFinished" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:NULL];

[queue addOperation:operation];

..... // launch the view controller
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"isFinished"]) {
NSLog(@"came in");
[operation removeObserver:self forKeyPath:@"isFinished"];
}
else
{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}

最佳答案

以下代码适用于我。我从 iOS 单 View 应用程序模板开始。这是我的:

@implementation SOAppDelegate
{
NSOperationQueue* queue;
NSOperation* operation;
}

- (void)CreateOperationWithContext: (id)foo
{
NSLog(@"Op ran");
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
queue = [NSOperationQueue new];
// operation = [NSInvocationOperation new]; // Commented this out because it's redundant with the next line
operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(CreateOperationWithContext:) object:[NSObject new]];
[operation addObserver:self forKeyPath:@"isFinished" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:NULL];
[queue addOperation:operation];
return YES;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"isFinished"])
{
NSLog(@"came in");
[operation removeObserver:self forKeyPath:@"isFinished"];
}
else
{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}

// ... rest of empty default app delegate methods here...

@end

在控制台中,我看到:

2013-08-13 08:04:15.150 TestOpKVO[71373:20b] Op ran
2013-08-13 08:04:21.903 TestOpKVO[71373:20b] came in

所以关于您的 -CreateOperationWithContext: 实现的一些事情引起了麻烦。也就是说,即使我将操作更改为抛出异常,我仍然会看到 KVO 通知被调用。

如果我是你,我会从这个非常基本的工作示例开始,然后一次进行一个步骤以使其适应你的实际代码,检查每一步以确保通知仍在工作。

一些提示:(可能与您遇到的问题无关,但是使用 KVO 的良好做法)

首先,在您的观察中使用 KVO 上下文。它更安全,更具确定性。查看answer I wrote over here了解详情。

其次,不要在调用 -observeValueForKeyPath:(或 -addObserver:... 任一个)对于正在通知的相同 keyPath。这有可能搞乱 KVO 的内部观察者数据结构,并可能导致非确定性崩溃,让您抓狂。查看answer I wrote over here了解详情。

关于ios - observeValueForKeyPath 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18189035/

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