【问题标题】:Is it possible to use a parent entity's property in the NSPredicate for a NSFetchRequest?是否可以在 NSPredicate 中为 NSFetchRequest 使用父实体的属性?
【发布时间】:2011-02-10 17:26:26
【问题描述】:

我有一个实体 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>'

【问题讨论】:

  • 您能否再描述一下您的实体层次结构并显示您正在使用的实际谓词?
  • 我已经用更具体的代码 sn-ps 更新了我的问题。

标签: iphone ios core-data nspredicate


【解决方案1】:

您需要拨打superentity吗?即

[NSPredicate predicateWithFormat:@"superentity.stringProperty = %@", someString];

或者您可以为您的获取请求获取实体 A 和 setIncludesSubentitles:YES

【讨论】:

  • 感谢您的回复。在这么长时间没有人回应的地方,我继续前进,我记得,我只是改变了我的等级制度。当我有时间时,我会尝试您的解决方案并相应地接受您的回答。
  • 我尝试使用 superentity.stringProperty 并得到以下错误:由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'keypath superentity.stringProperty not found in entity
  • 另外,setIncludesSubentities:YES 不是一个选项,因为我只希望结果中有 B 个实体。
【解决方案2】:

我刚刚在最新的 SDK (4.3) 中尝试了一个类似的示例,现在它可以工作了。我现在可以在子实体的谓词中使用父实体的属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    相关资源
    最近更新 更多