CoreData NSFetchRequest returns results, but the object section number returns 0

I'm currently trying to write my first CoreData project, and I'm very confused. I use the following code to configure the resulting result controller:

- (NSFetchedResultsController *)fetchedResultsController { if (_fetchedResultsController != nil) return _fetchedResultsController; Singleton *singleton = [Singleton sharedSingleton]; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"MapEntity" inManagedObjectContext:[singleton managedObjectContext]]; [fetchRequest setEntity:entity]; NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"sName" ascending:NO]; [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]]; [fetchRequest setFetchBatchSize:8]; // Finally check the results NSError *error; NSArray *fetchedObjects = [[singleton managedObjectContext] executeFetchRequest:fetchRequest error:&error]; for (MapEntity *map in fetchedObjects) { NSLog(@"Maps present in database: %@", map.sName); } NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[singleton managedObjectContext] sectionNameKeyPath:nil cacheName:@"Root"]; self.fetchedResultsController = theFetchedResultsController; _fetchedResultsController.delegate = self; return _fetchedResultsController; } 

I put in an NSLog to see if fetchRequest found the results, and it does:

 2013-01-28 11:20:14.735 WildMap_iOS[10931:16a03] Maps present in database: UK Capital Cities 2013-01-28 11:20:14.736 WildMap_iOS[10931:16a03] Maps present in database: The UK 2013-01-28 11:20:14.736 WildMap_iOS[10931:16a03] Maps present in database: Testing123 2013-01-28 11:20:14.736 WildMap_iOS[10931:16a03] Maps present in database: TestForPic 2013-01-28 11:20:14.736 WildMap_iOS[10931:16a03] Maps present in database: Scottish Map 2013-01-28 11:20:14.736 WildMap_iOS[10931:16a03] Maps present in database: Old Scotland 2013-01-28 11:20:14.736 WildMap_iOS[10931:16a03] Maps present in database: Old Scotland 2013-01-28 11:20:14.737 WildMap_iOS[10931:16a03] Maps present in database: Old Scotland 2013-01-28 11:20:14.737 WildMap_iOS[10931:16a03] Maps present in database: Field Concatination 

However, when I try to use my fetchedResultsController to set up my tableView numberOfRowsInSection as follows:

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { id sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section]; return [sectionInfo numberOfObjects]; } 

"return [sectionInfo numberOfObjects];" returns nil, although fetchRequest seems to be great for objects.

I used this code in order, however, I recently added an integer to mapEntity (iID), and this made me delete the simulator data as the structure of the saved data changes. After I deleted the data and restarted the application, the code no longer works.

Can someone help me understand why this is happening? I don’t think this is a naming convention as it was before, and fetchRequest returns the results.

+4
source share
2 answers

In addition to what @Levi suggested, you should not access the _fetchedResultsController instance _fetchedResultsController directly in numberOfRowsInSection (or in other table data sources). Always use the access property self.fetchedResultsController .

Reason: the get t fetchedResultsController creates, if necessary, the resulting result controller. Direct access to _fetchedResultsController may return nil .

Update: Another issue is benchmark caching. From the documentation:

Important: if you use a cache, you must call deleteCacheWithName: before modifying any query to select its predicate or sort descriptors. You should not reuse the same output result controller for multiple queries unless you set cacheName to nil .
...
If possible, the controller uses the cache to avoid the need to repeat the work performed in setting up any partitions and organizing the contents. Cache is supported in all runs of your application.

Therefore, you must either use cacheName:nil , or make sure that the cache is deleted when any FRC parameters change.

+4
source

Before the line return _fetchedResultsController; in your method, tell NSFerchedResultsController to execute the request:

 [self.fetchedResultsController performFetch:NULL]; 
+1
source

All Articles