2012-03-14 154 views
2

我在我的核心數據模型中擁有一對多關係。一餐可以有多種食物。我想用一頓飯中的食物填充一張桌子。所以如果第一餐含有蘋果,橙子和檸檬,那麼這張桌子就包含了這三種食物。核心數據一對多關係

任何幫助非常感謝,因爲我完全卡住了。

這裏是我的模型:

enter image description here

而且NSManagedObject類:

Meal.h

@class Food; 

@interface Meal : NSManagedObject 

@property (nonatomic, retain) NSString *name; 
@property (nonatomic, retain) NSSet *foods; 

@end 

@interface Meal (CoreDataGeneratedAccessors) 

- (void)addFoodsObject:(Food *)value; 
- (void)removeFoodsObject:(Food *)value; 
- (void)addFoods:(NSSet *)values; 
- (void)removeFoods:(NSSet *)values; 

@end 

Food.h

@class Meal; 

@interface Food : NSManagedObject 

@property (nonatomic, retain) NSString * name; 
@property (nonatomic, retain) Meal *meals; 

@end 

這裏是我試圖嘗試這個,但我有一些問題的代碼。

- (NSFetchedResultsController *)fetchedResultsController 
{  
    if (_fetchedResultsController != nil) 
     return _fetchedResultsController; 

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Food" inManagedObjectContext:_context]; 
    [fetchRequest setEntity:entity]; 

    [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"Meal.foods == %@", entity]]; 

    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO]; 
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]]; 
    [fetchRequest setFetchBatchSize:20]; 

    NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:_context sectionNameKeyPath:nil cacheName:@"Root"]; 

    self.fetchedResultsController = theFetchedResultsController; 
    _fetchedResultsController.delegate = self; 

    [fetchRequest release]; 
    [theFetchedResultsController release]; 

    return _fetchedResultsController;  
} 
+0

你在卡住?請告訴我們你的問題到底是什麼。 – sosborn 2012-03-14 02:58:38

+0

爲什麼在這裏使用一對多的關係?如果「食物」代表一種食物而不是特定的食物,那麼我希望給定的「食物」實例能夠與多餐相關。 – Caleb 2012-03-14 06:21:56

+0

你的問題解決了? – 2012-03-14 06:40:05

回答

3

你提到你有問題,但不是問題是什麼;但是你的謂詞NSFetchRequest對我來說並不合適。另外meals定義Food是複數,但關係是一對一 - 有點混淆。

您的UITableView應該從單個Meal填充Food,對吧?讓我們稱之爲Meal_thisMeal,則取謂語應該是:

NSPredicate *foodPredicate = [NSPredicate predicateWithFormat:@"meals == %@",_thisMeal]; 
[fetchRequest setPredicate:foodPredicate]; 

所以,您要提取的實體是Food;並且這個謂詞確保它們的meals屬性是您的UITableViewController正在查找的Meal

更新,以顯示如何獲取一個名爲Meal

要提取一個名爲「定製餐」 Meal

Meal *_thisMeal = nil; 
// this assumes that there should be only one Meal named "Custom Meal" 
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Meal"]; 
NSPredicate *fetchPredicate = [NSPredicate predicateWithFormat:@"name = %@",@"Custom Meal"]; 

NSError *fetchError = nil; 
NSArray *meals = [_context executeFetchRequest:request error:&fetchError]; 
if(fetchError) 
    NSLog(@"%s - ERROR while fetching Meal: %@,%@",__FUNCTION__,fetchError,[fetchError userInfo]); 
else if([meals count] != 0) 
    _thisMeal = [meals objectAtIndex:0]; 
+0

你能解釋一下這餐嗎?這條線會給我錯誤的餐食* _thisMeal。這條線應該是什麼? – Vikings 2012-03-14 11:11:08

+1

對不起,我的觀點是你的'UITableViewController'必須'知道'正在顯示什麼餐才能讓你的'NSFetchRequest'工作。據推測,它可能從上游某個地方得到一份膳食參考,也許在之前的表格視圖中被選中。爲了討論起見,我只是把它稱爲'_thisMeal',因爲我不知道在你的代碼中對特定餐的引用是什麼。 – FluffulousChimp 2012-03-14 11:19:50

+0

你能告訴我如何得到膳食_thisMeal?比方說,我想要一頓名爲「自定義膳食」的膳食,然後我要顯示自定義食物中的食物。這整個概念讓我感到困惑。 – Vikings 2012-03-14 11:29:09

0

你的膳食和食品類是錯誤的。這是最好的殺死那些讓XCode的自動重新生成 通過這些類(請確保您的實體被選中):

enter image description here

在你fetchedResultsController getter方法,殺線:

[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"Meal.foods == %@", entity]]; 

您通常會使用謂詞來過濾數據,例如包含香蕉的食物。你目前的謂詞目前並不合理。它說:「給我包含一種叫'實體'的食物,......沒有意義。

既然你想要吃飯......

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Food" inManagedObjectContext:_context]; 

應該是:

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Meal" inManagedObjectContext:_context]; 

(注意@"Meal")。

然後,您可以通過meal.foods訪問每頓飯中的食物。

這條線:
self.fetchedResultsController = theFetchedResultsController;
應該是:
_fetchedResultsController = theFetchedResultsController;
什麼你也基本上是調用這個方法,你目前在

+0

這些是Xcode生成的NSManageObject類 – Vikings 2012-03-14 12:08:14