【问题标题】:NSFetchedResultsController not updating tableNSFetchedResultsController 不更新表
【发布时间】:2015-09-13 17:43:25
【问题描述】:

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

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

我可以看到 beginUpdates,使用 Delete 类型调用了 didChangeObject,调用了 endUpdates()。我的缓存是 nil。

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

提前致谢!

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

【问题讨论】:

    标签: ios swift core-data swift2


    【解决方案1】:

    对于遇到相同问题的任何人,以下是解决方案:

    在你的:

    func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
    

    确保更新案例在插入案例之前......所以而不是这个:

      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)
                }
            }
    

    你应该有这个:

    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)
            }
        }
    

    让我头疼但终于找到了解决办法……

    【讨论】:

    • Swift (3) 不需要break。案件永远不会失败。顺序并不重要。
    • 这仍然是 iOS 10+ / Swift 4+ 的问题吗?
    【解决方案2】:

    它看起来像 iOS 8 错误,您可以在此处找到更多信息:https://forums.developer.apple.com/thread/11662#65178

    如果你想修复它,试试这个代码:

    public func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
    
        switch type {
        case NSFetchedResultsChangeType(rawValue: 0)!:
            break // iOS 8 bug - Do nothing if we get an invalid change type.
        case .Insert:
            // Insert here
            break
        case .Delete:
            // Delete here
            break
        case .Move:
            // Move here
            break
        case .Update:
            // Update here
            break
        }
    }
    

    【讨论】:

      【解决方案3】:

      就我而言,我不得不这样做:

      case .Update:
      

      case NSFetchedResultsChangeType(rawValue: 3)!:
      

      对于 didChangeObject:

          func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
          print(type.rawValue)
      
          switch type {
          case .Insert:
              print("didChangeObject.Insert")
              tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
          case .Delete:
              print("didChangeObject.Delete")
              tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
          case NSFetchedResultsChangeType(rawValue: 3)!:
              print("didChangeObject.Update")
              self.configureCell(tableView.cellForRowAtIndexPath(indexPath!)!, withObject: anObject as! NSManagedObject)
          case .Move:
              tableView.moveRowAtIndexPath(indexPath!, toIndexPath: newIndexPath!)
          default:
              return
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-14
        相关资源
        最近更新 更多