CoreData多对多关系(子)查询 [英] CoreData many-to-many relation (sub)query

查看:140
本文介绍了CoreData多对多关系(子)查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下结构的模型:




  • 文档实体有许多命令


  • 标签有许多文件
  • >


这是很重要的,因为这种类型的关系使用了额外的表。





我想要实现的是要求CoreData指定一个属于与之没有关系的文档的命令TagTypeUnwanted类型的标签,在单个查询中显然:)



我试图用一个谓词创建子查询:

  [NSPredicate predicateWithFormat:@SUBQUERY(document.tags,$ tag,$ tag.type ==%d)。@ count == 0,TagTypeUnwanted] 

不幸的是,它尝试与管理Document和Tag之间关系的中间表一起加入。所以它导致抱怨,它不能找到归档'类型'在那里,因为它只有Document id和标签id,这不是我的预期。

  [NSPredicate] predicateWithFormat:@NOT ANY document.tags.type ==%d,TagTypeUnwanted] 

这一个也不工作,因为它只留在SQL中,并且它在每个找到的文档中,由于与许多类型的标签的关系中找到的冲突。因此,它总是会找到至少一个记录,其中类型是非类型TagTypeUnwanted。



如果你喜欢更多的信息或生成的SQL或任何东西,只是写在评论。提前感谢!






看起来像NOT ANY和NONE都不能在Core Data中工作,检查它是一个已知的问题,自2007年以来,通常你做子查询解决这个问题,但似乎子查询在这种情况下不能处理多对多表



NOT ANY生成:AND NOT(Tag.type == TagTypeUnwanted),所以它只是接近SQL应该是什么样子。

解决方案

可能不是很有效,但是可能有趣的是,以下嵌套的SUBQUERY似乎工作:

  [NSPredicate predicateWithFormat:
@SUBQUERY(document,$ doc,SUBQUERY($ doc.tags,$ tag,$ tag.type ==%d)。@ count == 0)!= NULL,
TagTypeUnwanted]


I have a model with following structure:

  • Document entity has many Commands
  • Command belongs to Document

  • Document has many Tags

  • Tag has many Documents

This is important because of additional table used for this type of relation

Tag has also a type property which is an integer being an enum.

What I try to achieve is to ask CoreData for a Commands that belong to Documents which do not have a relation with a tag of type TagTypeUnwanted, in a single query obviously :)

I tried to make a subquery with a predicate:

[NSPredicate predicateWithFormat:@"SUBQUERY(document.tags, $tag, $tag.type == %d).@count == 0", TagTypeUnwanted]

unfortunatelly it tries to join with the middle-table that manages the relation between Document and Tag. So it results in complaining that it cannot find filed 'type' there, because it has only Document id and Tag id and this is not what I expected. It looks like it would work for every other relation than many-to-many, in this case however it's lost.

[NSPredicate predicateWithFormat:@"NOT ANY document.tags.type == %d", TagTypeUnwanted]

this one also doesn't work as it leaves only NOT in the SQL and it reaults in every Document being found because of relations with many types of tags. So it will always find at least one record where type is NOT type TagTypeUnwanted.

If you like more info or generated SQL or anything, just write it in a comment. Thanks in advance!


Looks like NOT ANY and NONE both are not working in Core Data, as far as I was able to check it is a known issue since 2007, normally you do subquery to workaround this, but it seems like subquery in this case cannot handle many-to-many table

NOT ANY generates: AND NOT (Tag.type == TagTypeUnwanted) so it is only close to how SQL should really look

解决方案

This is perhaps not very effective, but it might be interesting that the following nested SUBQUERY seems to work:

[NSPredicate predicateWithFormat:
    @"SUBQUERY(document, $doc, SUBQUERY($doc.tags, $tag, $tag.type == %d).@count == 0) != NULL",
    TagTypeUnwanted]

这篇关于CoreData多对多关系(子)查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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