Search code examples
objective-ccore-dataios5iphone-4nsfetchedresultscontroller

iPhone4 iOS5 NSFetchedResultsController how to pick batch size?


I got 5 fetched results controllers now, and am adding my first one to actually work with data, rather than just displaying it. I expect the controller to manage up to 150 objects. What batch size should I choose to work with up to 5 objects a time, in sequence? Is it 5?

  - (NSFetchedResultsController *)estimatorEventsController
    {
        if (__estimatorEventsController != nil)
        {
            return __estimatorEventsController;
        }

        /*
         Set up the fetched results controller.
         */
        // Create the fetch request for the entity.
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        // Edit the entity name as appropriate.
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"EstimatorEvent" inManagedObjectContext:self.managedObjectContext];

        [fetchRequest setEntity:entity];

        // Set the batch size to a suitable number.
        [fetchRequest setFetchBatchSize:36];

        // Edit the sort key as appropriate.
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"epoch" ascending:YES];
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

        [fetchRequest setSortDescriptors:sortDescriptors];

        // Edit the section name key path and cache name if appropriate.
        // nil for section name key path means "no sections".
        NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"EstimatorEvents"];
        aFetchedResultsController.delegate = self;
        self.estimatorEventsController = aFetchedResultsController;


        NSError *error = nil;
        if (![self.estimatorEventsController performFetch:&error])
        {

            NSLog(@"Unresolved error __remindersController %@, %@", error, [error userInfo]);
            //      abort();
        }

        return __estimatorEventsController;
    }    

I appreciate your help!


Solution

  • Alex,

    I have 800+ items in my fetched results controllers and don't batch the fetches. Don't worry about 150 items ... you know, premature optimization and all that.

    The other thing to remember is the the batch limit is on the fetch request not the fetched results controller. This distinction is important because the fetch request is a lower level item that can easily trigger a results array containing thousands of items. One must be careful with queries of such sizes. 150 rows is just not a big deal.

    Andrew