TableView 上的更新无效

Invalid update on TableView

Whosebug 的朋友们好。

我的应用程序上有一个聊天屏幕,它根据数组的实际大小执行插入和删除操作。看这个:

func addObject(object: Object?) {

    if comments == nil || object == nil || object?.something == nil || object?.anything == nil {
      return
    }

    self.objectsTableView.beginUpdates()

    if self.objects!.count == 10 {
      self.objects?.removeAtIndex(9)
      self.objectsTableView.deleteRowsAtIndexPaths([NSIndexPath(forRow : 9, inSection: 0)], withRowAnimation: .Right)
    }

    self.objects?.insert(object!, atIndex: 0)
    self.objectsTableView.insertRowsAtIndexPaths([NSIndexPath(forRow : 0, inSection: 0)], withRowAnimation: .Right)

    self.objectsTableView.endUpdates()

  }

但是经过一些压力测试,日志提示:

Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (10), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).

我不知道发生了什么,只有当对象插入非常极端时才会发生这种情况,比如每 0.2 秒一个。

有人知道我能做什么吗?

型号不匹配

The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (10), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted)

用通俗易懂的话来说,UITableView 认为你应该有 11 行:
10 更新前 + 1 插入。

number of rows contained in an existing section after the update (1)

...指的是 numberOfRowsInSection 正在为 0 部分返回 1,这表明 objects 数组不同步,假设您使用类似下面:

override func tableView(tableView: UITableView,
                        numberOfRowsInSection section: Int) -> Int {
    return objects.count
}

使用NSFetchedResultsController

一个干净的解决方案是使用 NSFetchedResultsController 作为模型和 UI 之间的接口。它对样板代码进行了深入研究,是确保线程安全的绝佳平台。 Documentation here.


注:

效果不错!单元格似乎旋转到顶部。
我无法使用您生成的 Gist 来破坏它,也无法安排多个并发测试。必须有一个 恶意访问 到您的 Object 数组。

演示

这个简化版本有效。只需将 doPlusAction 挂接到按钮操作并观察它循环:

class TableViewController: UITableViewController {
    var objects:[Int] = [0,1,2,3,4]
    var insertions = 5

    @IBAction func doPlusAction(sender: AnyObject) {
        tableView.beginUpdates()

        objects.removeAtIndex(4)
        tableView.deleteRowsAtIndexPaths([NSIndexPath(forRow: 4, inSection: 0)], withRowAnimation: .Right)
        objects.insert(insertions++, atIndex: 0)
        tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: .Right)
        tableView.endUpdates()

        let delay = 0.1 * Double(NSEC_PER_SEC) //happens the same with this too, when reach 100-150 items
        let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
        dispatch_after(time, dispatch_get_main_queue()) { () -> Void in
            self.doPlusAction(self)
        }
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return objects.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
        cell.textLabel!.text = "Cell \(objects[indexPath.row])"
        return cell
    }
}

解决问题的人的名字:Semaphore

错误仍然发生,但仅在列表中的项目很大时发生。我不知道可以是什么。

数据源协议:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let count = self.objects?.count ?? 0
    if self.semaphore != nil && semaphoreCode == BLOCKED_STATE {
      dispatch_semaphore_signal(self.semaphore!)
    }
    return count
  }

添加对象的方法:

func addObject(object: Object?) {

    if object == nil {
      return
    }

    if self.semaphore != nil {
      let tempSemaCode = dispatch_semaphore_wait(semaphore!, 100000)
      if tempSemaCode == BLOCKED_STATE {
        self.semaphoreCode = RELEASED_STATE
      }
    }

    if self.objects != nil && semaphoreCode != BLOCKED_STATE {

      var needsRemoveLastItem = false
      if self.objects!.count == 10 {
        self.objects?.removeAtIndex(9)
        needsRemoveLastItem = true
      }
      self.objects?.insert(object!, atIndex: 0)

      if self.objects!.count > 0 {
        self.objectsTableView.beginUpdates()
        if needsRemoveLastItem {
          self.objectsTableView.deleteRowsAtIndexPaths([NSIndexPath(forRow : 9, inSection: 0)], withRowAnimation: .Right)
        }
        self.objectsTableView.insertRowsAtIndexPaths([NSIndexPath(forRow : 0, inSection: 0)], withRowAnimation: .Right)
        self.objectsTableView.endUpdates()
        self.semaphore = dispatch_semaphore_create(BLOCKED_STATE)
      }

    }

  }