2

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!

Alex Stone
  • 46,408
  • 55
  • 231
  • 407

2 Answers2

2

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

adonoho
  • 4,339
  • 1
  • 18
  • 22
  • Do you mean that you do not set this property: [fetchRequest setFetchBatchSize:36];? – Alex Stone Nov 14 '11 at 22:06
  • Alex, Unless your 150 items are large blobs, then just get them all. The smallest iOS device ever gave you a 20 MB workspace to start. All Mac OS X devices were larger. One secret to CD performance is to get things into RAM and do your complex queries there. Hence, only limit CD when you know you aren't going to fit in RAM. Andrew – adonoho Nov 16 '11 at 12:46
1

I don't have a terribly informed answer, but it seems that most guides appear to be proposing that you load about two to three times as many cells as will be appearing on the screen at any one time, so if you have five visible in the table at a time, then perhaps 15? It is a balance between the benefits of not loading everything at once, versus not having too many fetch requests as they have some load and latency and making sure you already have some cells that are not yet on screen pre-fetched to be responsive during sudden rapid scrolling.

Duncan Babbage
  • 19,972
  • 4
  • 56
  • 93
  • The scrolling aspect is fairly easy. I'm interested in the calculations aspect. How would I pick the batch size for calculation purposes, not just scrolling? – Alex Stone Nov 12 '11 at 15:03