我有两个核心数据实体,Articles
和Favorite
. Articles
与 具有多对多关系Favorite
。首先,我Articles
成功插入了所有对象。
现在,我试图在“收藏夹”实体中插入 ArticleID,但我不能。要么以空关系插入记录,要么以“文章”实体中的新记录插入记录。
我认为我应该先获取Articles
实体中的相关记录,然后使用它插入,Favorite
但我不知道该怎么做。我当前的代码:
NSManagedObjectContext *context =[appDelegate managedObjectContext] ;
favorite *Fav =[NSEntityDescription insertNewObjectForEntityForName:@"favorite" inManagedObjectContext:context];
Articles * Article = [NSEntityDescription insertNewObjectForEntityForName:@"Articles" inManagedObjectContext:context];
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Articles" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSPredicate *secondpredicate = [NSPredicate predicateWithFormat:@"qid = %@",appDelegate.GlobalQID ];
NSPredicate *thirdpredicate = [NSPredicate predicateWithFormat:@"LangID=%@",appDelegate.LangID];
NSPredicate *comboPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects: secondpredicate,thirdpredicate, nil]];
[fetchRequest setPredicate:comboPredicate];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *info in fetchedObjects) {
// ?????????????????????????
}
}
任何建议,将不胜感激。