Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all entries of a NSFetchedResultsController/NSManagedObjectContext?

I have a nice working iphone app that works with core data. I use a NSFetchedResultsController/NSManagedObjectContext as described in various tutorials.

Now I want to extends my app and add some more features. The problem I need to build up an array with objects that have informations from my data.

I somehow need to get a list of all data I have in my context.

I thought I could make an approach similar to the way I get the data for the UITableView.

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

This one fails, because I do have actually multiple sections. Now I could go through all sections and make my own IndexPath to access my data with :

MyData *info = [_fetchedResultsController objectAtIndexPath:indexPath];

But I think there is another way I just have not found yet and I hope someone can help me out here.

Thanks a lot.

like image 854
eemceebee Avatar asked Oct 09 '10 22:10

eemceebee


2 Answers

are you just looking for a method to get all objects from you NSFetchedResultsController? If so, use this.

NSArray *fetchedData = [_fetchedResultsController fetchedObjects];

if you have more than 1 entity build a fetchrequest for each entity. Something like this should give you all your objects.

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];
like image 118
Matthias Bauch Avatar answered Nov 02 '22 23:11

Matthias Bauch


If you wanted to get all of the objects for a particular section you could do this:

NSArray *sectionObjects = [_fetchedResultsController.sections[section_number] objects];
like image 37
Mr. T Avatar answered Nov 03 '22 00:11

Mr. T