开发者

How to Insert Core Data Record Related to Another Without Duplication

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 setPredic开发者_Python百科ate:comboPredicate];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *info in fetchedObjects) { 
        // ?????????????????????????

    }

}

Any suggestions would be appreciated.


First, make sure you have a reciprocal i.e. two way relationship between Article and Favorite. Something like this:

Article{
  favorites<-->>Favorite.article
}

Favorite{
  article<<-->Article.favorites
}

Defining reciprocal relationships in Core Data means that setting the relationship from one side automatically sets it for the other.

So, to set a new Favorite object for a newly created Article object you would just:

Favorite *fav =[NSEntityDescription insertNewObjectForEntityForName:@"favorite" inManagedObjectContext:context];
Articles *article = [NSEntityDescription insertNewObjectForEntityForName:@"Articles" inManagedObjectContext:context];
[article.addFavoriteObject:fav];
//... or if you don't use custom NSManagedObject subclasses
[[article mutableSetValueForKey:@"favorites"] addObject:fav];

If either the Article object or the Favorite object already exist, you would fetch the object first but setting the relationship would work exactly the same way.

The key is to make sure you have the reciprocal relationship so that the managed object context knows to set the relationship in both objects.


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..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜