为单个KVC更改接收2个KVO通知 [英] Receiving 2 KVO notifications for a single KVC change

查看:70
本文介绍了为单个KVC更改接收2个KVO通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用KVC / KVO为大学项目创建自定义绑定实现(需要自定义,因为我想做的事情超出了绑定可以做,包括在iOS上运行)。



我有一个绑定控制器注册一个对象上的许多键(使用addObserver:forKeyPath:options:context :)的KVO通知,我会收到通知。但是,我收到每个更改的两个通知。我有一个想法的解决方法,但我宁愿工作,为什么这发生和纠正它!



有没有任何想法为什么这可能发生?我确定我只有一次注册每个通知,一次注销一次会导致两个通知都停止。



感谢。



编辑:



我有一些代码的请求,所以我会把一些。目前有点粗糙,



这是我观察的属性之一(我知道这有点奇怪,但这个类本质上暴露了NSManagedObject的某些属性作为其自身的属性):

   - (void)setName:(NSString *)name 
{
[self willChangeValueForKey:@name];
[contact setFirstName:name];
[self didChangeValueForKey:@name];
}

这是我用来观察属性的代码,并且只在一个地方):

  [viewModel addObserver:self 
forKeyPath:@name
options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial)
context:viewController];

更改该属性后的任何调用,例如:

  viewModel.name = @Joe; 
[viewModel setName:@Joe];
[viewModel setValue:@JoeforKey:@name];

会导致以下方法:

   - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 

解决方案



每次调用两次, div>

您正在这样做:

   - (void)setName:(NSString *)name 
{
[self willChangeValueForKey:@name];
[contact setFirstName:name];
[self didChangeValueForKey:@name];
}

但是(从它的声音) c> NSManagedObject 子类。这意味着Cocoa将尝试自动为您发送KVO通知。你也是通过发送自己的补充。解决方案:




  • 覆盖 + automaticallyNotifiesObserversForKey:以返回 NO

  • 将您的方法更改为:



    - )setName:(NSString *)name
    {
    [contact setFirstName:name];
    }



I'm using KVC/KVO to create a custom bindings implementation for a University project (it needs to be custom as I want to do things beyond what bindings can do, including running on iOS).

I have a 'bindings controller' that registers for KVO notifications on a number of keys on an object (using addObserver:forKeyPath:options:context:) and I do receive notifications. However I am receiving two notifications for each change. I have an idea for a workaround, but I would rather work out why this is happening and correct it!

Does anyone have any ideas why this might be happening? I'm certain I have only registered each notification a single time, and deregistering a single time causes both of the notifications to stop.

Thanks.

Edit:

I have a request for some code, so I'll put some in. It's a bit rough at the moment, it's essentially still a proof of concept, so bear with me.

This is one of the properties I am observing (I know it's a bit strange, but this class essentially exposes certain properties of an NSManagedObject as properties of itself):

- (void)setName:(NSString *)name
{
    [self willChangeValueForKey:@"name"];
    [contact setFirstName:name];
    [self didChangeValueForKey:@"name"];
}

This is the code I use to observe that property (confirmed to only run once, and only in a single place):

[viewModel addObserver:self
            forKeyPath:@"name"
               options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial)
               context:viewController];

Any calls after that which change that property, e.g.:

viewModel.name = @"Joe";
[viewModel setName: @"Joe"];
[viewModel setValue: @"Joe" forKey: @"name"];

will cause the method:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context

to be called twice, with the same arguments each time.

解决方案

You're doing this:

- (void)setName:(NSString *)name
{
    [self willChangeValueForKey:@"name"];
    [contact setFirstName:name];
    [self didChangeValueForKey:@"name"];
}

But (by the sounds of it) from a non-NSManagedObject subclass. This means that Cocoa will be attempting to send KVO notifications automatically for you. You're supplementing that by sending your own too. Solutions:

  • Override +automaticallyNotifiesObserversForKey: to return NO
  • Change your method to:

    - (void)setName:(NSString *)name { [contact setFirstName:name]; }

这篇关于为单个KVC更改接收2个KVO通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆