Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios addNotificationBlock{RealmCollectionChange}在UITableView中崩溃_Ios_Swift_Uitableview_Realm - Fatal编程技术网

Ios addNotificationBlock{RealmCollectionChange}在UITableView中崩溃

Ios addNotificationBlock{RealmCollectionChange}在UITableView中崩溃,ios,swift,uitableview,realm,Ios,Swift,Uitableview,Realm,我正在使用Realm进行Swift,并将数据加载到UITableView中。当我进入屏幕时,大约有200个数据对象正在逐渐下载,因此在我的测试中,在tableview显示之后,会有大量的数据插入到UITableView中。我正在使用Realm示例尽可能紧密地添加NotificationBlock和RealmCollectionChange,并且在这个过程中偶尔会发生两个单独的崩溃 ***由于未捕获的异常“RLMEException”而终止应用程序,原因:“只能从runloops中添加通知块。”

我正在使用Realm进行Swift,并将数据加载到UITableView中。当我进入屏幕时,大约有200个数据对象正在逐渐下载,因此在我的测试中,在tableview显示之后,会有大量的数据插入到UITableView中。我正在使用Realm示例尽可能紧密地添加NotificationBlock和RealmCollectionChange,并且在这个过程中偶尔会发生两个单独的崩溃

  • ***由于未捕获的异常“RLMEException”而终止应用程序,原因:“只能从runloops中添加通知块。”
  • 即使我强调要从ViewController类中的主线程中提取所有数据,也会发生此崩溃

  • ***在-[UITableView\u endCellAnimationsWithContext:],/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3512.30.14/UITableView.m:1720中断言失败 ****由于未捕获异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:节0中的行数无效。”。更新(27)后现有节中包含的行数必须等于更新(17)前该节中包含的行数,加上或减去从该节中插入或删除的行数(插入9行,删除0行),加上或减去移入或移出该节的行数(0迁入,0迁出)。'
  • 这场崩溃是在我更换了

    tableView.reloadData()
    

    在我的addNotificationBlock的.Update()部分

    我想这是因为我没有完全理解这个库的内部机制

    以下是我的代码供参考:

    self.exhibits = DataManager.getAllExhibitsSorted("id")
    token = self.exhibits?.addNotificationBlock { (changes: RealmCollectionChange) in
      switch changes {
      case .Initial(_):
    
        self.exhibits = DataManager.getAllExhibitsSorted("id")
        self.exhibitListTableView.reloadData()
        break
      case .Update(_, let deletions, let insertions, let modifications):
    
        // Query results have changed, so apply them to the UITableView
        self.exhibitListTableView.beginUpdates()
        self.exhibitListTableView.insertRowsAtIndexPaths(insertions.map { NSIndexPath(forRow: $0, inSection: 0) },
          withRowAnimation: .Automatic)
        self.exhibitListTableView.deleteRowsAtIndexPaths(deletions.map { NSIndexPath(forRow: $0, inSection: 0) },
          withRowAnimation: .Automatic)
        self.exhibitListTableView.reloadRowsAtIndexPaths(modifications.map { NSIndexPath(forRow: $0, inSection: 0) },
          withRowAnimation: .Automatic)
        self.exhibitListTableView.endUpdates()
    
        break
      case .Error:
        NSLog("Error in notificationBlock")
        break
      }
    }
    

    听起来你可能把事情复杂化了一点

    Realm
    结果
    对象是实时和自动更新的。这意味着在主运行循环的下一次迭代中,对其基础对象所做的更改将自动更新,因此无需对其执行手动重新获取。在那里的代码中,您在
    初始
    更改noti中分配了
    self.expensions
    例如,在生成令牌之后,这可能会导致您在此处遇到一些问题。如果删除该行,它将继续工作

    我建议您仔细检查代码,确保只分配一次
    self.exhibies
    ,并且更改通知方法仅应用于该代码


    如果这还不能解决问题,请告诉我。

    Ugh。这在大多数情况下都能解决问题,但当我在下载过程中打开和关闭wifi时,仍然会出现NSInternalInconsistency异常。这可能是notificationBlock.Update和self.exhibies中存在的值之间的竞争条件。甚至只是一些关于如何捕获该异常的帮助t错误可能就足够了。更新中的任何内容都不应该抛出错误,所以我不能用do-catch来包围它。值得一提的是,我遇到了完全相同的问题。你解决过这个问题吗?这似乎是领域内的某个竞争条件。我正在使用领域文档中显示的100%代码。。。
    self.exhibits = DataManager.getAllExhibitsSorted("id")
    token = self.exhibits?.addNotificationBlock { (changes: RealmCollectionChange) in
      switch changes {
      case .Initial(_):
    
        self.exhibits = DataManager.getAllExhibitsSorted("id")
        self.exhibitListTableView.reloadData()
        break
      case .Update(_, let deletions, let insertions, let modifications):
    
        // Query results have changed, so apply them to the UITableView
        self.exhibitListTableView.beginUpdates()
        self.exhibitListTableView.insertRowsAtIndexPaths(insertions.map { NSIndexPath(forRow: $0, inSection: 0) },
          withRowAnimation: .Automatic)
        self.exhibitListTableView.deleteRowsAtIndexPaths(deletions.map { NSIndexPath(forRow: $0, inSection: 0) },
          withRowAnimation: .Automatic)
        self.exhibitListTableView.reloadRowsAtIndexPaths(modifications.map { NSIndexPath(forRow: $0, inSection: 0) },
          withRowAnimation: .Automatic)
        self.exhibitListTableView.endUpdates()
    
        break
      case .Error:
        NSLog("Error in notificationBlock")
        break
      }
    }