我有兩個核心數據實體Articles
和Favorite
。 Articles
與Favorite
有多對多關係。首先,我成功插入了所有的Articles
對象。如何在不復制的情況下插入與另一個相關的核心數據記錄
現在,我試圖插入ArticleID在「收藏」實體,但我不能。無論是記錄是插入一個空的關係,或者它被插入「Articles」實體中的新記錄。
我認爲我應該首先獲得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) {
// ?????????????????????????
}
}
任何意見,將不勝感激。
感謝TechZen .. 我有已經反轉文章和最喜歡的關係.. 文章的對象先前插入,現在我想插入相關的收藏對象。 我已經將這行添加到循環中: [[Fatwa mutableSetValueForKey:@「FavArticle」] addObject:Fav]; 並獲取此錯誤: 由於未捕獲的異常'NSUnknownKeyException',原因:'[<文章0xc9dfec0> valueForUndefinedKey:]終止應用程序:實體文章不是密鑰值編碼兼容的密鑰「FavFArticle」。 如何使用fetched Articles對象插入喜歡的對象?? – smaiibnauf 2011-05-04 22:47:12
錯誤表示您沒有爲'Articles'實體/類定義的/ spelled「FavArticle」屬性。這通常只是拼寫錯誤導致您拼錯關鍵名稱的結果,例如真實姓名就像「favArticle」或「favArticles」。 – TechZen 2011-05-05 17:38:19
爲什麼從文章到收藏的關係是1-> N? – 2011-12-05 12:47:01