sql – NSFetchRequest不捕获具有已更改属性的对象

sql – NSFetchRequest不捕获具有已更改属性的对象,第1张

概述我使用SQL存储在MacOsX 10.6上遇到了CoreData的奇怪问题.我有一个名为Family的NSManagedObject子类,其属性名称和关系personList连接到另一个名为Person的NSManagedObject子类,具有属性firstname和inverse relationship family.一个人只有一个家庭,一个家庭可以有几个人. 假设我有一个Family对象系列 我使用sql存储在MacOsX 10.6上遇到了CoreData的奇怪问题.我有一个名为Family的NSManagedobject子类,其属性名称和关系personList连接到另一个名为Person的NSManagedobject子类,具有属性firstname和inverse relationship family.一个人只有一个家庭,一个家庭可以有几个人.

假设我有一个Family对象系列,指向与2个人(John和Jane)连接的家庭’Doe’,我执行以下请求:

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人的阵列:Jane和John,姓氏Doe.现在,如果我使用其合成访问器更新Family,在我的情况下:

[family setname:@"Wheat"]

我不能在使用相同的获取请求获取Person列表之后.结果是[数组计数]为0.

如果我将谓词更改为以下行,它将再次起作用:

[request setPredicate:[nspredicate predicateWithFormat:@"family=%@",family]];

所以就好像Predicate没有使用该系列属性名的更新版本,即使我将NSFetchRequest设置为默认值(所以includesPendingChanges返回YES).这对我来说毫无意义.看起来NSFetchRequest找到了family对象,但是没有看到它的值family.name已经更新,没有保存,并且在内存中的managedobjectContext中.当然,如果我保存商店,那么它再次起作用.

任何的想法?我已经通过Mac文档,无法理解为什么会失败.

解决方法 我认为这里的关键是理解获取请求.它从持久性存储中检索数据,因此很明显,如果您没有保存到持久性存储,它将无法找到该数据.如果考虑到这一点,您描述的情况完全合乎逻辑.

来自核心数据编程指南:

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.

总结

以上是内存溢出为你收集整理的sql – NSFetchRequest不捕获具有已更改属性的对象全部内容,希望文章能够帮你解决sql – NSFetchRequest不捕获具有已更改属性的对象所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1003636.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存