2011-08-30 25 views
1

天兒真好所有當觸發下載StackMob初始數據到CoreData應用

我工作的一個CoreData驅動應用程序,它與空CoreData店,我從一個​​應用填充開始。

我有一個UITableView的子類提取&提供我的數據,因爲我希望它,但我有點困惑時,我應該最好從StackMob獲取最初的數據。當我在applicationDidFinishLaunching中填充我的CoreData存儲(從小plist文件&僅用於測試視圖)時,我的應用程序花費了很長時間顯示默認屏幕&我期望從Web上獲取實際數據的時間會更長。我在考慮我的UITableView子類改變這種方法...

- (NSFetchedResultsController *)frc 
{ 
    if (_frc) return _frc; 

    ... 

    return _frc; 
} 

到...

- (NSFetchedResultsController *)frc 
{ 
    if (_frc) return _frc; 

    ... 

    if ([[_frc fetchedObjects] count] == 0) { 
     // Spawn NSOperation to get initial data from StackMob. 
     // & use it to populate my CoreData store. 
    } 

    return _frc; 
} 

在這種情況下,我做的NSOperation的子類,我可以重新用於後續的數據更新。 我正在檢查[[_frc fetchedObjects] count] == 0,因爲我正在從實體中提取所有數據。

是一種很好的方法嗎?如果不是更好的方法?

我希望能提供一些用戶體驗,就像我在某些應用程序中看到的一樣,我使用的項目出現在「主頁」屏幕上,因爲它被下載到CoreData商店中添加到&。

乾杯& TIA,佩德羅:)

+0

您是否按照真棒核心數據教程之一這將引導您通過使用Xcode定義CoreData架構(如http://cocoadevcentral.com/articles/000085.php)的端到端場景,或者您已經交付了您的數據結構並且只是在代碼界面上工作? – bmike

+0

我的代碼基於Apple示例和一些在線教程(包括Marcus Zarra),非常接近以下ScanPlayGames提供的內容。我編輯了這個問題,使我真正要求更清楚。 – Pedro

回答

1

首先,創建一個NSPredicate來從核心數據的信息(假設它的NSSet中,在這種情況下):

NSMutableSet yourMutableDataSetYouGotFromCoreData = [self.yourCoreDataObject mutableSetValueForKey:@"yourCoreDataSetData"]; 

NSPredicate *yourDataFilter = [NSPredicate predicateWithFormat:@"SELF IN %@",yourMutableDataSetYouGotFromCoreData]; 

接下來,建立fetche使用謂詞

// Fetched results controller 
NSFetchedResultsController *yourFetchedResults = [YOURCOREDATAOBJECT fetchRequestAllGroupedBy:nil 
                           withPredicate:supplyRegisterFilter]; 

下一頁結果控制器,將該信息給你的表

現在210
[self.yourTable updateTableData:yourFetchedResults]; 

,在你的餐桌,在那裏你創建單元格數據內容 - 使用這樣得到的數據從您的獲取結果控制器的

-(void) updateTableData:(NSFetchedResultsController*)fetched 
{ 
     self.fetchedResultsController = fetched; 

     if(self.fetchedResultsController) 
      [self.tableView reloadData]; 

} 


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [self.fetchedResultsController.fetchedObjects count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView 
    cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    YourCustomCellType *cell = (YourCustomCellType *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (!cell) 
    { 
     NSArray *topLevelItems = [cellLoader instantiateWithOwner:self options:nil]; 
     cell = [topLevelItems objectAtIndex:0]; 
    } 

    [self configureCellData atIndexPath:indexPath]; 

    return cell; 
} 

- (void) configureCellData:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{ 
    YourCustomCellType *customCell = (YourCustomCellType *)cell; 
    id obj = [[fetchedResultsController fetchedObjects] objectAtIndex:indexPath.row]; 
    [customCell setCellDataFromId:obj]; 
} 
+0

我正在做所有這些事情(除了我沒有謂詞,因爲我想要查詢實體中的所有項目)並且它在我有數據時有效。它也適用於沒有數據並顯示一個空的UITableView。我的應用需要從StackMob下載一些數據,第一次運行和什麼時候觸發,這是我的問題。我會編輯這個問題,使其更清晰。 – Pedro

+0

感謝您的幫助。我編輯了這個問題,並將它放在下巴上,因爲沒有足夠清楚地開始。 – Pedro

相關問題