NSFetchedResultsController 不更新表

Cyb*_*erK 3 core-data ios swift swift2

我已将我的应用程序升级到 Swift 2.0,从那时起,NSFetchedResultsController 在 iOS 8.4 上无法正常运行(在 iOS 9 中,它按预期工作)

场景: - 添加一个新实体 - 行出现在表视图中 - 实体的属性已更改,因此它不应与 fetchedtesultscontroller 的谓词匹配 - 该行不会从表视图中消失...

我可以看到 beginUpdates,使用删除类型调用 didChangeObject,并调用 endUpdates() 。我的缓存为零。

这是 iOS 8.4 的 xCode 7 中的已知错误吗?(有趣的是,有时它确实有效,但大多数时候它不起作用,这对我的应用程序至关重要......)

提前致谢!

ps我尝试了他们在网上说的 if (indexPath != newIndexPath) 东西,但结果相同......

Cyb*_*erK 5

对于遇到同样问题的人,解决方案如下:

在你的:

func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
Run Code Online (Sandbox Code Playgroud)

确保更新案例位于插入案例之前...所以不要这样:

  func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
            switch type {
            case .Insert:
                self.tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: UITableViewRowAnimation.Fade);
            case .Move:
                if(!indexPath!.isEqual(newIndexPath!)) {
                    self.tableView.moveRowAtIndexPath(indexPath!, toIndexPath: newIndexPath!);
                }
            case .Update:
                let cell = self.tableView.cellForRowAtIndexPath(indexPath!) as! UITableViewCell;
                self.configureCell(cell, atIndexPath: indexPath!);
                self.tableView.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Fade);
            case .Delete:
                self.tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Fade)
            }
        }
Run Code Online (Sandbox Code Playgroud)

你应该有这个:

func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
        switch type {
        case .Update:
            let cell = self.tableView.cellForRowAtIndexPath(indexPath!) as! UITableViewCell;
            self.configureCell(cell, atIndexPath: indexPath!);
            self.tableView.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Fade);
        case .Insert:
            self.tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: UITableViewRowAnimation.Fade);
        case .Move:
            if(!indexPath!.isEqual(newIndexPath!)) {
                self.tableView.moveRowAtIndexPath(indexPath!, toIndexPath: newIndexPath!);
            }
        case .Delete:
            self.tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Fade)
        }
    }
Run Code Online (Sandbox Code Playgroud)

搞得我很头疼,不过最后还是找到了解决办法……

  • Swift (3) 不需要 `break`。案件永远不会落空。顺序并不重要。 (4认同)