NSManagedObject和KVO与文档 [英] NSManagedObject and KVO vs Documentation
问题描述
我有一个自定义的 NSManagedObject
子类,比如说 Person
。我也有一个 UIView
注册 -addObserver:forKeyPath:options:context:
观察 Person
,其中一些是像name一样的持久化,而其他的只是与核心数据无关的非常符合KVO的访问器,如喝酒。
@interface Person:NSManagedObject
{
BOOL drinking;
}
@property(nonatomic,retain)NSString * name;
@property(nonatomic,readonly)BOOL饮酒;
@end
@implementation人
@dynamic名称;
...
- (void)getDrunk {
[self willChangeValueForKey:@drinking];
drinking = YES;
[self didChangeValueForKey:@drinking];
}
...
@end
一切正常。每当我发送 -getDrunk
或设置 name
属性时,视图会收到通知。除了当我阅读 NSManagedObject
docs时,我是一个快乐的人:
+(BOOL)automaticallyNotifiesObserversForKey:(NSString *)key
事实2. NSManagedObject的默认实现对模型化属性返回NO(否)。如果接收者提供关键值的键值观察更改通知的自动支持,则为NO。
< ,对于未建模的属性为YES。
现在我正在努力从文档中解析上述两个事实。检查事实2很容易,类Person确实返回@的否和喝的是。但是,然后,当名称更改时如何通知视图? KVO docs清楚地说,
使用自动观察器通知,没有必要使用willChangeValueForKey:和didChangeValueForKey:
因此,如果Person从<$ c $返回NO c> + automaticallyNotifiesObserversForKey:对于@name,似乎我必须手动包装名称setter在 will / didChangeValueForKey:
for KVO上班。但是,KVO工作很好。我缺少什么?在 NSManagedObject
的重写 + automaticallyNotifiesObserversForKey:
,并记录它如果似乎没有改变标准KVO行为是什么意义?
请帮我恢复理智。
NSManagedObject
确实提供了 name
属性的实现(以及 code>和
- setName:
方法)。我假设Core Data提供的实现确实包括对 willChangeValueForKey:
和 didChangeValueForKey:
的调用。
所以,虽然KVO是自动的,在你不必做任何事情,使其工作的意义上,我想是它是 willChangeValueForKey:
和 didChangeValueForKey:
正在由 NSManagedObject
,提供动态属性实现。
I have a custom NSManagedObject
subclass, say, Person
. I also have a UIView
registered with -addObserver:forKeyPath:options:context:
to observe various properties of a Person
, some of which are persistent like "name" and others are just dumb KVO-compliant accessors unrelated to Core Data, like "drinking".
@interface Person : NSManagedObject
{
BOOL drinking;
}
@property (nonatomic, retain) NSString* name;
@property (nonatomic, readonly) BOOL drinking;
@end
@implementation Person
@dynamic name;
...
- (void) getDrunk {
[self willChangeValueForKey: @"drinking"];
drinking = YES;
[self didChangeValueForKey: @"drinking"];
}
...
@end
Everything works. Whenever I send -getDrunk
or set the name
property, the view does get notified. I'm a happy man except when I read NSManagedObject
docs which state:
+ (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key
Fact 1. YES if the receiver provides automatic support for key-value observing change notifications for key, otherwise NO.
Fact 2. The default implementation for NSManagedObject returns NO for modeled properties, and YES for unmodeled properties.
Now I'm trying hard to parse the above two facts from the docs. Checking Fact 2 is easy and class Person indeed returns NO for @"name" and YES for @"drinking". But then, how does the view get notified when the name changes? KVO docs clearly say,
Using automatic observer notifications, it is not necessary to bracket changes to a property with invocations of willChangeValueForKey: and didChangeValueForKey: when mutating properties via key-value coding and key-value coding compliant methods.
So, if Person returns NO from +automaticallyNotifiesObserversForKey:
for @"name", it would seem that I have to manually wrap the name setter in will/didChangeValueForKey:
for KVO to work. However, KVO works just fine. What am I missing? What's the point in NSManagedObject
's overriding +automaticallyNotifiesObserversForKey:
and documenting it if does not seem to change standard KVO behaviour?
Please, help me regain my sanity.
Well, NSManagedObject
does provide an implementation for the name
property (as well as the - name
and - setName:
methods). I would assume that the implementations provided by Core Data do include calls to willChangeValueForKey:
and didChangeValueForKey:
.
So, although the KVO is "automatic" in the sense that you didn't have to do anything to make it work, I would imagine that it is not automatic in the sense that willChangeValueForKey:
and didChangeValueForKey:
are being called by the methods in NSManagedObject
that provide the dynamic property implementations.
这篇关于NSManagedObject和KVO与文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!