【发布时间】:2015-04-06 10:28:05
【问题描述】:
我有两个实体模型“内容”和“课程内容”,它们之间没有关系。两个实体都具有 NSNumber 类型的共同属性“contentID”。我从给定谓词的“内容”实体中获取所有内容对象,并将这些对象存储在内容数组中。我成功地这样做了。 现在,我想从 contentIds 存在于内容数组中的“LessonsContent”实体中获取所有课程内容对象。
我正在使用以下代码:
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"(contentType == %@)", @"Games"];
NSError * error;
NSFetchRequest * request = [[NSFetchRequest alloc] initWithEntityName:NSStringFromClass([Content class])];
[request setPredicate:predicate];
[request setReturnsObjectsAsFaults:NO];
NSArray *contents = [[DELEGATE managedObjectContext] executeFetchRequest:request error:&error];
if(error){ NSLog(@"Error in fatching data");}
//predicate = [NSPredicate predicateWithFormat:@"Content.contentID IN %@", contents];
predicate = [NSPredicate predicateWithFormat:@"(SUBQUERY(contents, $x, $x.Content.contentID IN %@).@count != 0)",contents];
request = [[NSFetchRequest alloc] initWithEntityName:NSStringFromClass([LessonsContent class])];
[request setPredicate:predicate];
NSArray * mappingArray = [[DELEGATE managedObjectContext] executeFetchRequest:request error:&error];
NSLog(@"mappingArray %@", mappingArray);
但是应用程序崩溃了。请提出适当的解决方案。 提前致谢。
【问题讨论】:
-
在第二个谓词中使用 IN 子句。 [stackoverflow][1] [1]:stackoverflow.com/questions/6130900/…
-
contentID 属性是 NSNumber 类型。我已经尝试过了,但它不起作用。
-
我会考虑使用
NSCompoundPredicate和方便的方法andPredicateUsingSubpredicates:见here
标签: ios entity-framework core-data