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

sma*_*auf 5 core-data duplication objective-c one-to-many relationship

我有两个核心数据实体,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 (NSManagedObject *info in fetchedObjects) { 
        // ?????????????????????????

    }

}
Run Code Online (Sandbox Code Playgroud)

任何建议,将不胜感激.

sma*_*auf 0

我通过创建新的 Article 对象解决了这个问题:

Articles *NewObj = [fetchedObjects objectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)

并用它来插入关系:

    [Fav setFavArticles:NewObj];
     [NewObj setArticlesFav:Fav];
Run Code Online (Sandbox Code Playgroud)

非常感谢TechZen..