Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 setPredicate:comboPredicate];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *info in fetchedObjects) { 
        // ?????????????????????????

    }

}

Any suggestions would be appreciated.

like image 877
smaiibnauf Avatar asked Oct 11 '22 03:10

smaiibnauf


1 Answers

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.

like image 156
TechZen Avatar answered Oct 14 '22 04:10

TechZen