2010-10-09 64 views
14

我有一個很好的工作iphone應用程序與核心數據。我使用NSFetchedResultsController/NSManagedObjectContext,如各種教程中所述。如何獲取NSFetchedResultsController/NSManagedObjectContext的所有條目?

現在我想擴展我的應用程序並添加一些更多的功能。我需要用包含數據信息的對象構建數組。

我以某種方式需要獲取我在上下文中所有數據的列表。

我想我可以做一個類似於我得到UITableView數據的方法。

id <NSFetchedResultsSectionInfo> sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section]; 

這一個失敗,因爲我確實有多個部分。現在,我可以去通過所有部分,並讓我自己IndexPath與訪問我的數據:

MyData *info = [_fetchedResultsController objectAtIndexPath:indexPath]; 

但我覺得這是我還沒有找到,我希望有人能幫助我在這裏的另一種方式。

非常感謝。

回答

40

你只是在尋找一種方法來獲取你的所有對象NSFetchedResultsController?如果是這樣,使用這個。

NSArray *fetchedData = [_fetchedResultsController fetchedObjects]; 

如果你有超過1個實體建立每個實體fetchrequest

。像這樣的東西應該會給你所有的東西。

NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:self.entityName inManagedObjectContext:self.managedObjectContext]; 
[request setEntity:entity]; 
NSError *error; 
NSArray *results = [self.managedObjectContext executeFetchRequest:request error:&error]; 
1

如果你想獲得的所有對象的特定部分,你可以這樣做:

NSArray *sectionObjects = [_fetchedResultsController.sections[section_number] objects]; 
相關問題