是否可以在 NSFetchRequest 的 NSPredicate 中使用父实体的属性?
我有一个如下所示的实体 A:
@interface A : NSManagedObject
{
}
@property (nonatomic, retain) NSString *stringProperty;
它有一个如下所示的子实体 B:
@interface B : A
{
}
我想使用 A 中存储的属性对 B 执行提取。类似这样:
NSManagedObjectContext *context = [delegate mainManagedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"B" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"stringProperty = %@", someString];
[request setPredicate:pred];
这可能吗?我目前收到以下错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath stringProperty not found in entity <NSSQLEntity B id=26>'
I have an entity A like the following:
@interface A : NSManagedObject
{
}
@property (nonatomic, retain) NSString *stringProperty;
that has a subentity B like this:
@interface B : A
{
}
I would like to perform a fetch on B using a property stored in A. Something like this:
NSManagedObjectContext *context = [delegate mainManagedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"B" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"stringProperty = %@", someString];
[request setPredicate:pred];
Is this possible? I am currently getting the following error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath stringProperty not found in entity <NSSQLEntity B id=26>'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要调用
superentity
吗?即或者您可以为您的提取请求获取实体 A 和
setInincludesSubentitles:YES
。Do you need to call
superentity
? ieOr you could fetch entity A and
setIncludesSubentitles:YES
for your fetch request.我刚刚在最新的 SDK (4.3) 中尝试了类似的示例,现在它可以工作了。我现在可以在子实体的谓词中使用父实体的属性。
I just tried a similar example in the latest SDK (4.3) and it now works. I can now use the parent entities' properties in a predicate for a subentity.