获取核心数据实体,但不是子实体 [英] Fetching Core Data entities, but not sub-entities

查看:105
本文介绍了获取核心数据实体,但不是子实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个实体以层次结构组织, Parent 是根实体, Child code> Parent 。我想设置一个 NSArrayController 只获取 Parent 的实体,而不是 Child

Say I have entities organized in a hierarchy with Parent being the root entity and Child being a subclass of Parent. I'd like to setup an NSArrayController to fetch only entities of Parent, but not Child.

如果在Interface Builder中将数组控制器的实体名称设置为 Parent ,它获取所有 Parent Child 实体。我最初尝试在Interface Builder中将数组控制器的fetch谓词设置为:

If you set the Entity Name of the array controller in Interface Builder to Parent, it fetches all Parent and Child entities. I originally tried setting the array controller's fetch predicate in Interface Builder to:

entity.name == "Parent"

这适用于XML存储,但当我切换到SQLite存储时,它不再工作。我收到以下错误:

This worked for an XML store, but when I switched to a SQLite store, it no longer worked. I get the following error:

keypath entity.name not found in entity <NSSQLEntity xxx>

作为一个解决方法,我设置一个过滤谓词> entity.name 如上所述)在我的 awakeFromNib 中只过滤父实体。显然,一旦实体在内存中,该谓词是有效的,但是你不能在支持SQL的获取谓词中使用它。

As a work around, I am setting up a filter predicate (with the same entity.name predicate as above) in my awakeFromNib to filter only Parent entities. Apparently, that predicate is valid once the entities are in memory, but you can't use it in a SQL-backed fetch predicate.

是否有一种方法只能获取实体,但不是实体使用与SQLite存储一起工作的获取谓词?

Is there a way to fetch only Parent entities, but not Child entities using a fetch predicate that works with a SQLite store? It seems wasteful to pull in entities that you're only going to ignore with the filter predicate.

推荐答案

我尝试使用 includeSubentities ,但它最终不能完全工作。结果是,如果您将自动准备内容设置为是,从而绕过了自定义获取谓词,则对子实体的更改会导致数组控制器的内容更新而不进行提取。回复跟踪显示 setContent:正在响应MOC通知。

I tried using includesSubentities, but it ended up not working completely. It turns out changes to subentities cause the array controller's content to get updated without doing a fetch if you have "automatically prepares content" set to "Yes", thus bypassing the custom fetch predicate. The backtrace shows setContent: being called in response to MOC notifications.

我发现要做的唯一方法这可靠地是使用过滤谓词。

The only way I've found to do this reliably is to use a filter predicate.

这篇关于获取核心数据实体,但不是子实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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