在 Core Data / Objective-C 中有很多关系
我正在尝试使用 Objective-C 重新创建 has_many :through 关系(就像在 Rails 中一样),如果核心数据由 SQLite 支持,这一定是可能的,对吗?
我可以在 2 秒内为此编写原始 SQL...
无论如何,基本上这里是我的架构的一个子集:导师组----<分配>----类别,我需要获取的是分配给特定导师组的所有类别。
到目前为止,这是我的查询:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(SUBQUERY(Allocation, $row, $row.tutor_group == %@ AND $row.category == <CURRENT_ROW>).@count > 0)", tutor_group];
有没有办法在子查询中表示当前行的对象(我在其中编写了
?我尝试过使用 self< /code> 但这不起作用。
我想要执行子查询的表名称?
另外,我假设我放置 Allocation
的位置是
I'm trying to recreate a has_many :through relationship (like in Rails) with Objective-C, if Core Data is backed by SQLite, this must be possible right? <rant>
I could write the raw SQL for this in 2 seconds... </rant>
Anyway, basically here is a sub-set of my schema: TutorGroup ----< Allocation >---- Category, what I need to get is all categories that are allocated to a particular tutor group.
Here is my query so far:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(SUBQUERY(Allocation, $row, $row.tutor_group == %@ AND $row.category == <CURRENT_ROW>).@count > 0)", tutor_group];
Is there a way to represent the current row's object in a sub-query (where I've written <CURRENT_ROW>
? I've tried using self
but that doesn't work.
Also, I'm assuming where I've put Allocation
is the table name I want to perform the sub query on?
Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定它是否有效,但请尝试以下操作:
然后使用 predicateWithFormat 传入您的导师组,并在类别上运行它。
I'm not sure it will work, but try this:
Then pass in your tutor group using predicateWithFormat, and run it on Category.
CoreData 中的标准方法是拥有所有逆关系。那么您根本不必使用谓词。
确实建议使用反向关系...请参阅
因此,假设实体
TutorGroup
具有一对多关系allocations
,并且Allocation
具有一对一关系类别
。然后
参见集合运算符的讨论 此处。
通常,当您使用 Core Data 时,您不应该使用 CoreData 由 SQLite 支持的知识。这让您安心。
The standard way in CoreData is to have all the inverse relationships. Then you don't have to use predicates at all.
Having inverse relationships is really recommended... see this official document.
So suppose the entity
TutorGroup
has a to-many relationshipallocations
, andAllocation
has a to-one relationshipcategory
.Then
See the discussion of collection operators here.
Typically, when you use Core Data, you shouldn't use the knowledge that CoreData is backed by SQLite. That gives you a peace of your mind.