Search code examples
iphonecore-datansmanagedobjectcontext

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.


Solution

  • 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];