I have two core data entities, Articles
and Favorite
. Articles
has To-Many relationship to Favorite
. First, I inserted all Articles
object successfully.
Now, I'm trying to insert ArticleID in “Favorite” entity but I cant. Either the record is inserted with an empty relationship or it is inserted with new record in “Articles” entity.
I think that I should be getting the related record in Articles
entity first and then using it to insert in Favorite
but I'm not sure how to do this. My current code:
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) {
// ?????????????????????????
}
}
Any suggestions would be appreciated.
I solved it by creating new Article object:
Articles *NewObj = [fetchedObjects objectAtIndex:0];
and use it to insert relationship :
[Fav setFavArticles:NewObj];
[NewObj setArticlesFav:Fav];
thanks very much TechZen..