0
我有兩個實體模型「Content」和「LessonsContent」,它們之間沒有任何關係。兩個實體都具有NSNumber類型的共同屬性「contentID」。我從給定謂詞的「內容」實體中提取所有內容對象,並將這些對象存儲在內容數組中。我成功地這樣做了。 現在,我想從內容數組中存在contentIds的「LessonsContent」實體中提取所有LessonContent對象。子查詢謂詞核心數據
我使用下面的代碼:
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 %@)[email protected] != 0)",contents];
request = [[NSFetchRequest alloc] initWithEntityName:NSStringFromClass([LessonsContent class])];
[request setPredicate:predicate];
NSArray * mappingArray = [[DELEGATE managedObjectContext] executeFetchRequest:request error:&error];
NSLog(@"mappingArray %@", mappingArray);
但應用程序崩潰。請提出適當的解決方案 在此先感謝。
對第二個謂詞使用IN子句。 [計算器] [1] [1]:http://stackoverflow.com/questions/6130900/filter-core-data-results-by-property-in-array – 2015-04-06 10:32:12
內容識別屬性是NSNumber類型。我已經嘗試過,但它不起作用。 – 2015-04-06 10:36:39
我會考慮使用'NSCompoundPredicate'和便捷方法'andPredicateUsingSubpredicates:'see [here](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSCompoundPredicate_Class/) – andrewbuilder 2015-04-07 08:10:10