NSFetchRequest不捕获具有更改属性的对象 [英] NSFetchRequest not catching objects that have a changed property
问题描述
我遇到一个奇怪的问题与CoreData在MacOsX 10.6使用SQL存储。我有一个 NSManagedObject
子类,名为 Family
,属性为 name
连接到另一个 NSManagedObject
子类( Person
)的关系 personList
属性 firstname
和反向关系 family
。 人只有一个
家庭
,且家庭
有几个人。
I have run into a weird problem with CoreData on MacOsX 10.6 using an SQL store. I have an NSManagedObject
subclass called Family
with attribute name
and a relationship personList
connected to another NSManagedObject
subclass called Person
with attribute firstname
and inverse relationship family
. A Person
has only one family
, and a family
can have several Person
s.
说我有一个家庭
对象 family
指向与其连接的 Person
(John和Jane)的家庭'Doe'以下请求:
Say I have a Family
object family
pointing to the family 'Doe' with 2 Person
(John and Jane) connected to it and I do the following request:
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:[NSEntityDescription entityForName:@"Person" inManagedObjectContext:managedObjectContext]];
[request setPredicate:[NSPredicate predicateWithFormat:@"family.name=%@",[family name]]];
NSArray *array = [managedObjectContext executeFetchRequest:request error:&error];
我得到一个2的数组与2个人:Jane和John,与家庭名字Doe。现在,如果我使用其合成的访问器更新家庭,在我的情况下:
I get an array of size 2 with the 2 persons: Jane and John, with Family name Doe. Now, if I update the Family using its synthesized accessor, in my case:
[family setName:@"Wheat"]
我得不到 Person
相同的提取请求。结果是 c> c $
I cannot after get the list of Person
using the same fetch request. The results is an [array count]
of 0
.
如果我将谓词更改为以下行,它会再次工作:
If I change the predicate to the following line, it works again:
[request setPredicate:[NSPredicate predicateWithFormat:@"family=%@",family]];
这就好像谓词没有使用家族的属性名称的更新版本,即使我将 NSFetchRequest
设置为默认值(因此 includesPendingChanges
返回YES)。这对我没有意义。看起来 NSFetchRequest
找到 family
对象,但是没有看到它的值 family .name
已更新,未保存,位于内存中的 managedObjectContext
中。当然,如果我保存商店,那么它再次工作。
So it is as if the Predicate is not using the updated version of the property name of the family, even though I have the NSFetchRequest
set to the defaults (so includesPendingChanges
returns YES). That makes no sense to me. It seems that the NSFetchRequest
finds the family
object, but fails to see that its value family.name
has been updated, not saved, and is in the managedObjectContext
in memory. Of course, if I save the store, then it works again.
任何想法?我已经通过Mac文档,看不到为什么会失败。
Any idea? I have been through the Mac documentation and cannot see why this would fail.
推荐答案
我认为这里的关键是了解抓取请求。它从持久存储检索数据,所以很清楚,如果你没有保存到持久存储,它不会找到那个数据。
I think the key here is understanding the fetch request. It retrieves data from the persistent store, so clearly, if you did not save to the persistent store, it will not find that data. The situation you describe is entirely logical if you take that into account.
从核心数据编程指南中可以看出:
From the Core Data Programming Guide:
您不能使用基于瞬态属性的谓词获取
(虽然您可以使用瞬态属性在内存中过滤
)。此外,在提取和
之间存在一些交互的类型 - 有关详细信息,请参阅商店类型和行为。至
总结,如果您执行fetch直接,你应该
通常不添加基于Objective-C的谓词或排序描述符到
获取请求。相反,你应该应用这些结果的
fetch。如果你使用一个数组控制器,你可能需要子类化
NSArrayController,所以你可以不通过排序描述符到
持久化存储,而是在你的数据取得
后进行排序。
You cannot fetch using a predicate based on transient properties (although you can use transient properties to filter in memory yourself). Moreover, there are some interactions between fetching and the type of store—for details, see "Store Types and Behaviors." To summarize, though, if you execute a fetch directly, you should typically not add Objective-C-based predicates or sort descriptors to the fetch request. Instead you should apply these to the results of the fetch. If you use an array controller, you may need to subclass NSArrayController so you can have it not pass the sort descriptors to the persistent store and instead do the sorting after your data has been fetched.
这篇关于NSFetchRequest不捕获具有更改属性的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!