核心数据keyPathsForValuesAffectingValueForKey仅调用关系,而不是属性 [英] Core Data keyPathsForValuesAffectingValueForKey only calling relationships, not attributes

查看:498
本文介绍了核心数据keyPathsForValuesAffectingValueForKey仅调用关系,而不是属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Core Data来建模具有属性和关系的实体。我想让一个属性依赖于另外两个关系。



Core Data常见问题及其他几个使用 +(NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key 这个目的。但是,这似乎只是为我所有的关系属性,但不是为任何属性。此外 keyPathsForValuesAffecting< key> 永远不会为我的任何属性调用。



这是正常的行为我错过了什么?如何基于其他属性或属性计算一个属性,例如。一个距离,通过设置一个startLocation和一个stopLocation?

解决方案

看起来像一个找到解决我自己的问题后, 。



重新读取方法 keysPathsForValuesAffectingValueForKey:中的方法的讨论 NSKeyValueObserving协议参考

a>,我意识到以下句子的含义:


当键的观察者注册接收类的实例时,键值观察本身会自动观察同一实例的所有键路径,并且当这些键路径的值发生变化时,会向观察者发送键的更改通知。


总之,您的实例应该有一个观察者观察属性< key> 的变化:

  [myInstance addObserver:myObserver forKeyPath:attributeKey options:nil context:nil]; 

一旦你注册了一个observer,协议会调用 keysPathsForValuesAffectingValueForKey 为您的特定属性键。如果此方法返回非空的一组关键路径,则除了通知您对属性的任何直接更改外,KVO还会为您的属性发出更改通知(如果对这些关键路径中的任何一个进行更改)。



关系键会自动调用,因为Core Data已经使用观察器保持最新的反向关系。



在特定情况下,您希望属性取决于同一实体中的另一个属性或关系,您必须:


  1. 在awakeFromInsert:方法中使用 addObserver:forKeyPath:options:context:添加观察者

  2. 实现 keyPathsForValuesAffectingValueForKey: keyPathsForValuesAffecting< key>

  3. 实现 observeValueForKeyPath:ofObject:change:context


I am using Core Data to model an entity which has both attributes and relationships. I would like to make one of the attributes dependent on two other relationships.

The Core Data FAQ and several other examples use +(NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key for this purpose. However, this only seems to be called for all of my relationship properties, but not for any of the attributes. In addition keyPathsForValuesAffecting<key> never gets called for any of my attributes.

Is this the normal behaviour or am I missing something? How can I calculate one attribute based on other attributes or properties, eg. a distance, by setting a startLocation and a stopLocation?

解决方案

It looks like a found the solution to my own problem after a couple of days and it turns out I was missing something.

After rereading the discussion of the method keysPathsForValuesAffectingValueForKey: in the NSKeyValueObserving Protocol Reference, I realized the meaning of the following sentence:

When an observer for the key is registered with an instance of the receiving class, key-value observing itself automatically observes all of the key paths for the same instance, and sends change notifications for the key to the observer when the value for any of those key paths changes.

In short, your instance should have an observer observing changes to your attribute <key>:

[myInstance addObserver:myObserver forKeyPath:attributeKey options:nil context:nil];

As soon as you have an observer registered, the protocol will call keysPathsForValuesAffectingValueForKey for your specific attribute key. If this method returns a non-empty set of key paths, KVO will emit a change notification for your attribute, if a change is made to any of these key paths, in addition to notifying you of any direct change to your attribute.

Relationship keys do get called automatically, because Core Data already uses observers to keep inverse relationships up to date.

In the particular case where you want to have an attribute depend on another attribute or relationship within the same entity, you will have to:

  1. Add an observer in the awakeFromInsert: method using addObserver:forKeyPath:options:context:
  2. Implement keyPathsForValuesAffectingValueForKey: or keyPathsForValuesAffecting<key>
  3. Implement observeValueForKeyPath:ofObject:change:context for your attribute key path to act on the relevant change notifications, i.e. updating your attribute value.

这篇关于核心数据keyPathsForValuesAffectingValueForKey仅调用关系,而不是属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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