小编sma*_*auf的帖子

如何在不重复的情况下插入与另一个相关的核心数据记录

我有两个核心数据实体,ArticlesFavorite.Articles与To-Many有关系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 …
Run Code Online (Sandbox Code Playgroud)

core-data duplication objective-c one-to-many relationship

5
推荐指数
1
解决办法
5333
查看次数