- (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; }
我在NSLog中查看fetchRequest是否正在查找结果,它确实:
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
但是,当我尝试使用我的fetchedResultsController来设置我的tableView numberOfRowsInSection时,如下所示:
- (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];’返回nil,即使fetchRequest好像正在拾取对象.
我使用这个代码很好,但是我最近在mapEntity(iID)中添加了一个整数,它让我删除了模拟器数据,因为存储数据的结构已经改变了.删除数据并重新启动应用程序后,代码不再有效.
任何人都可以帮助我理解为什么会这样吗?我不认为它是以前工作的命名约定,而fetchRequest返回结果.
解决方法
原因:fetchedResultsController getter方法在必要时创建获取的结果控制器.直接访问_fetchedResultsController可能会返回nil.
更新:另一个问题是获取结果控制器缓存.从文档:
Important: If you are using a cache,you must call
deleteCacheWithName:
before changing any of the fetch request,its
predicate,or its sort descriptors. You must not reuse the same
fetched results controller for multiple queries unless you set thecacheName
tonil
.
…
Where possible,a controller uses a cache to avoid the need to repeat work performed in
setting up any sections and ordering the contents. The cache is maintained across launches of your application.
因此,您应该使用cacheName:nil或确保在更改FRC的任何参数时删除缓存.