5

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.

TechZen
  • 63,819
  • 15
  • 116
  • 144
smaiibnauf
  • 63
  • 1
  • 4

2 Answers2

4

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.

TechZen
  • 63,819
  • 15
  • 116
  • 144
  • Thanks TechZen.. I have already inversed relationship between Articles and favorite .. the object of Articles is inserted previously and now I want to insert related favorite object . I have added this line into loop : [[Fatwa mutableSetValueForKey:@"FavArticle"] addObject:Fav]; and get this error : Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: the entity Article is not key value coding-compliant for the key "FavFArticle".' how to use fetched Articles object to insert favorite object ??.. – smaiibnauf May 04 '11 at 22:47
  • The error means that you don't have an attribute called/spelled "FavArticle" defined for the `Articles` entity/class. This is usually just the result of a typo that causes you to misspell the key name e.g. the real name is something like "favArticle" or "favArticles". – TechZen May 05 '11 at 17:38
  • Why does the relationship from Article to Favorite is 1-> N? – Eduardo Coelho Dec 05 '11 at 12:47
  • Because that is what the parent post's author said it was. The relationship doesn't have to be 1->N and the reciprocal doesn't have to be 1->1 but that is the most common pattern. – TechZen Dec 05 '11 at 23:25
0

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

smaiibnauf
  • 63
  • 1
  • 4