2013-05-01 117 views
1

我有實體的核心數據模型稱爲SeasonInfo包含了一堆的屬性,其中之一是一個屬性叫做(INT 16)。 SeasonInfo包含一個一對多的關係,名爲GameInfo(在SeasonInfo中定義的關係名稱被稱爲遊戲)。抓取一對多的關係數據和核心數據

我填充的數據庫包含一堆季節(每年一個)...然後每個季節都有一個遊戲列表 - 每個季節一堆遊戲。 SeasonInfo < ---- >>的GameInfo

在我的TableView位指示我用下面的訪問SeasonInfo給定年份:

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

// Set up the cell... 
SeasonInfo *info = [seasonInfoArray objectAtIndex:indexPath.row]; 

//Put the Year as the Main title of the Row 
yearLabel.text =[NSString stringWithFormat: @"%@", info.year]; 

,似乎工作偉大的..但現在我無法弄清楚如何訪問特定賽季的比賽。從這個表格視圖中,當我選擇特定年份時,我想將一個NSArray遊戲傳遞給另一個Table View控制器......我知道如何在視圖之間傳遞數據,我無法弄清楚如何填充在我轉換到計劃視圖之前的「遊戲陣列」。

回答

0

如果您只需要獲取特定季節的遊戲,那麼您可以在獲取之前使用謂詞(例如,這隻適用於int 32,將字符串格式說明符(%d)更改爲int16所需的任何內容):

NSPredicate * equalPredicate = [NSPredicate predicateWithFormat:@「year ==%d」,year]; [fetchRequest setPredicate:equalPredicate];

如果你不想這樣做,並希望收集所有的東西,那麼在你的executeFetchRequest完成後,你在成功完成塊中,你將得到一個NSArray *結果。

for (SeasonInfo * seasonInfo in results) 
{ 
    if ([seasonInfo year] == <whatever year you want>) 
    { 
     //save this seasonInfo for use, this contains the games you're interested in. 
    } 
} 

之後,得到在seasonInfo

for (GameInfo * gameInfo in [seasonInfo games]) 
{ 
    // Do what's needed with the gameInfo. 

} 
+0

感謝您的幫助 – 2013-05-08 00:40:55

1

如果您已經在覈心數據中正確創建了關係,那麼您可以通過它的屬性訪問遊戲關係。

因此,info.games應爲該SeasonInfo返回一個GameInfo的NSSet。

+0

感謝您的幫助特定的遊戲... ...能得到這方面的工作,此代碼:NSSet中* gameSet = info.games; – 2013-05-08 00:40:13