加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – CoreData NSFetchRequest返回结果,但对象的节数返回0

发布时间:2020-12-14 17:39:40 所属栏目:百科 来源:网络整理
导读:我目前正在尝试编写我的第一个CoreData项目并且非常困惑.我使用以下代码来设置一个获取的结果控制器: - (NSFetchedResultsController *)fetchedResultsController { if (_fetchedResultsController != nil) return _fetchedResultsController; Singleton *s
我目前正在尝试编写我的第一个CoreData项目并且非常困惑.我使用以下代码来设置一个获取的结果控制器:

- (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返回结果.

解决方法

除了@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 the
cacheName to nil.

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的任何参数时删除缓存.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读