• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - observeValueForKeyPath 没有被调用

[复制链接]
菜鸟教程小白 发表于 2022-12-12 13:35:07 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

这是我写的代码:

-(BOOL)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)launchOptions
{
    ........//initialization

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

    operation = [[NSInvocationOperation alloc]initWithTarget:self     selectorselector(CreateOperationWithContext object:context];

    [operation addObserver:self forKeyPath"isFinished" optionsNSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:NULL];

    [queue addOperationperation];

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

-(void)observeValueForKeyPathNSString *)keyPath ofObjectid)object changeNSDictionary *)change contextvoid *)context
{
    if ([keyPath isEqualToString"isFinished"]) {
        NSLog(@"came in");
        [operation removeObserver:self forKeyPath"isFinished"];
    }
    else
    {
        [super observeValueForKeyPath:keyPath ofObjectbject change:change context:context];
    }
}



Best Answer-推荐答案


以下代码对我有用。我从 iOS 单 View 应用程序模板开始。这是我所拥有的:

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

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

- (BOOL)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)launchOptions
{
    queue = [NSOperationQueue new];
    // operation = [NSInvocationOperation new]; // Commented this out because it's redundant with the next line
    operation = [[NSInvocationOperation alloc] initWithTarget:self selectorselector(CreateOperationWithContext object:[NSObject new]];
    [operation addObserver:self forKeyPath"isFinished" optionsNSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:NULL];
    [queue addOperationperation];
    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 ofObjectbject 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:... 的调用中调用 -removeObserver:forKeyPath: > 任一)对于被通知的相同 keyPath。这有可能弄乱 KVO 的内部观察者数据结构,并可能导致非确定性崩溃,让您抓狂。见 answer I wrote over here了解详情。

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

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap