如何在父实体的获取请求的谓词中引用子实体名称? [英] How can you reference child entity name in a predicate for a fetch request of the parent entity?

查看:17
本文介绍了如何在父实体的获取请求的谓词中引用子实体名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为抽象基础对象创建一个复杂的谓词.我想对不同的继承实体进行单独的谓词查询并关闭子实体类型,下面的示例是我想要做的,但是,我无法找到引用实体名称或键入的方法谓词.

I have the need to create a complex predicate for an abstract base object. I want to have separate predicate queries for different inheriting entities and key off the sub-entity type, the example below is what I would like to do, however, I have not been able to find a way to reference the entity name or type in the predicate.

NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"MyCommonObjectBase" inManagedObjectContext:myContext];

NSPredicate *subclassAPredicate = [NSPredicate predicateWithFormat:@"someValue > %@ && entityName = %@", 100, @"SubclassA"];
NSPredicate *subclassBPredicate = [NSPredicate predicateWithFormat:@"someValue < %@ && entityName = %@", 50, @"SubclassB"];

request.predicate = [NSCompoundPredicate orPredicateWithSubpredicates:[NSArray arrayWithObjects:subclassAPredicate, subclassBPredicate, nil]];

推荐答案

我在 http://bugreport 上收到了 Apple 的回复.apple.com 问题编号 7318897:

I got a response from Apple at http://bugreport.apple.com problem number 7318897:

entity.name 不是模型属性,因此不合法.它在二进制存储上偶然工作.获取子实体的正确方法是在 NSFetchRequest 上执行此操作,并使用或不使用 setIncludesSubentities.

entity.name is not a modeled property, so it's not legal. it works by accident on the binary store. The correct way to fetch subentities is to do so on the NSFetchRequest and use either setIncludesSubentities or not.

所以似乎正确的解决方案是有单独的获取请求并在执行后合并结果集.

So it seems the proper solution is to have separate fetch requests and to merge the result sets after execution.

这篇关于如何在父实体的获取请求的谓词中引用子实体名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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