A block of code that is divided between numberOfRowsInSection: and cellForRowAtIndexPath: should only be called once. numberOfRowsInSection always called before the tableView tries to map the cells, so you must create an NSArray object into which you can save the results of the select query, and then use this array when rendering your cells:
@implementation FooTableViewController { NSArray *_privateArray; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { [[UIManagedDocumentSingletonHandler sharedDocumentHandler] performWithDocument:^(FCUIManagedDocumentObject *document) { NSManagedObjectContext * context = document.managedObjectContext; NSFetchRequest * request = [NSFetchRequest fetchRequestWithEntityName:@"FCObject"]; NSPredicate * searchStringPredicate = nil; if (searchFilterString) { searchStringPredicate = [NSPredicate predicateWithFormat:@"word BEGINSWITH[c] %@",searchFilterString]; } request.predicate = searchStringPredicate; request.shouldRefreshRefetchedObjects = YES; NSError * error; _privateArray = [context executeFetchRequest:request error:&error]; }]; return _privateArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"FCCell"; FCardCell *cell = (FCCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
I'm not sure about the top of my head if you need to do something special with NSArray to set it inside a block (a la __block ).
The main reason for this is that you need to make sure that in 100% of cases when the dataset used to determine the number of rows is the same size as when you created your cells. If they do not match, you will crash. In addition, since you do not have a block, you do not need to send UITableViewCell updates.
Finally, if the UIDocumentStateClosed is causing problems, you must either filter them out of your NSFetch results (an additional predicate, if required) or have code to manage them more efficiently in cellForRowAtIndexPath:
source share