
- (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 CitIEs2013-01-28 11:20:14.736 WildMap_iOS[10931:16a03] Maps present in database: The UK2013-01-28 11:20:14.736 WildMap_iOS[10931:16a03] Maps present in database: Testing1232013-01-28 11:20:14.736 WildMap_iOS[10931:16a03] Maps present in database: TestForPic2013-01-28 11:20:14.736 WildMap_iOS[10931:16a03] Maps present in database: Scottish Map2013-01-28 11:20:14.736 WildMap_iOS[10931:16a03] Maps present in database: old Scotland2013-01-28 11:20:14.736 WildMap_iOS[10931:16a03] Maps present in database: old Scotland2013-01-28 11:20:14.737 WildMap_iOS[10931:16a03] Maps present in database: old Scotland2013-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返回结果.
解决方法 除了@Levi建议的内容之外,您不应直接在numberOfRowsInSection(或其他表视图数据源方法)中访问实例变量_fetchedResultsController.始终使用属性访问器self.fetchedResultsController.原因: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 thecachenametonil.
…
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的任何参数时删除缓存.
总结以上是内存溢出为你收集整理的ios – CoreData NSFetchRequest返回结果,但对象的节数返回0全部内容,希望文章能够帮你解决ios – CoreData NSFetchRequest返回结果,但对象的节数返回0所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)