Swift 分组的UITableView和领域通知

Swift 分组的UITableView和领域通知,swift,uitableview,realm,Swift,Uitableview,Realm,我有一个UITableView,它有两个部分。每个部分显示查询特定领域对象的结果 var contentRequests: Results<UESocketPostRequest>? { return try? CustomRealm.realm().objects(UESocketPostRequest.self).filter("completed = false AND content != nil").sorted(byKeyPath: "content.created

我有一个
UITableView
,它有两个部分。每个部分显示查询特定领域对象的结果

var contentRequests: Results<UESocketPostRequest>? {
   return try? CustomRealm.realm().objects(UESocketPostRequest.self).filter("completed = false AND content != nil").sorted(byKeyPath: "content.createdAt", ascending: false)
}

var mediaUploads: Results<UEMediaUploadRequest>? {
    return try? CustomRealm.realm().objects(UEMediaUploadRequest.self).filter("finished = false").sorted(byKeyPath: "media.content.createdAt", ascending: false)
} 
然后,我为我的每个领域结果设置了
NotificationToken
,并更新如下表视图:

func startContentToken() {
    self.contentToken = self.contentRequests?.observe({ [weak self] (changes) in
        switch changes {
        case .error:
            break
        case .initial:
            let indexSet = IndexSet(integer: 0)
            self?.tableView.reloadSections(indexSet, with: .automatic)
        case .update(let content, deletions: let deletions, insertions: let insertions, modifications: let modifications):
            self?.tableView.beginUpdates()
            self?.tableView.insertRows(at: insertions.map({ IndexPath(row: $0, section: 0) }),
                                       with: .automatic)
            self?.tableView.deleteRows(at: deletions.map({ IndexPath(row: $0, section: 0)}),
                                       with: .automatic)
            self?.tableView.reloadRows(at: modifications.map({ IndexPath(row: $0, section: 0) }),
                                       with: .automatic)
            self?.tableView.endUpdates()
        }
    })
}

func startMediaToken() {
    self.mediaToken = self.mediaUploads?.observe({ [weak self] (changes) in
        switch changes {
        case .error:
            break
        case .initial:
            let indexSet = IndexSet(integer: 1)
            self?.tableView.reloadSections(indexSet, with: .automatic)
        case .update(let media, deletions: let deletions, insertions: let insertions, modifications: let modifications):
            self?.tableView.beginUpdates()
            self?.tableView.insertRows(at: insertions.map({ IndexPath(row: $0, section: 1) }),
                                       with: .automatic)
            self?.tableView.deleteRows(at: deletions.map({ IndexPath(row: $0, section: 1)}),
                                       with: .automatic)
            self?.tableView.reloadRows(at: modifications.map({ IndexPath(row: $0, section: 1) }),
                                       with: .automatic)
            self?.tableView.endUpdates()
        }
    })
}
由于此日志消息或类似内容封装的
UITableView
不一致异常,我经常会崩溃:

***由于未捕获异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:节1中的行数无效。”。更新后现有节中包含的行数(4)必须等于更新前该节中包含的行数(0),加上或减去从该节中插入或删除的行数(0插入,0删除),加上或减去移入或移出该节的行数(0移入,0移出).


我是否做错了什么,或者Realm不是以这种方式设计用于分组的
UITableView

您需要做的是维护每个部分的手动计数。在域通知块中接收插入或删除时更新计数。返回
numberOfRowsInSection
中的计数。此处的更多信息:

您需要做的是维护每个部分的手动计数。在域通知块中接收插入或删除时更新计数。返回
numberOfRowsInSection
中的计数。更多信息请点击此处:

func startContentToken() {
    self.contentToken = self.contentRequests?.observe({ [weak self] (changes) in
        switch changes {
        case .error:
            break
        case .initial:
            let indexSet = IndexSet(integer: 0)
            self?.tableView.reloadSections(indexSet, with: .automatic)
        case .update(let content, deletions: let deletions, insertions: let insertions, modifications: let modifications):
            self?.tableView.beginUpdates()
            self?.tableView.insertRows(at: insertions.map({ IndexPath(row: $0, section: 0) }),
                                       with: .automatic)
            self?.tableView.deleteRows(at: deletions.map({ IndexPath(row: $0, section: 0)}),
                                       with: .automatic)
            self?.tableView.reloadRows(at: modifications.map({ IndexPath(row: $0, section: 0) }),
                                       with: .automatic)
            self?.tableView.endUpdates()
        }
    })
}

func startMediaToken() {
    self.mediaToken = self.mediaUploads?.observe({ [weak self] (changes) in
        switch changes {
        case .error:
            break
        case .initial:
            let indexSet = IndexSet(integer: 1)
            self?.tableView.reloadSections(indexSet, with: .automatic)
        case .update(let media, deletions: let deletions, insertions: let insertions, modifications: let modifications):
            self?.tableView.beginUpdates()
            self?.tableView.insertRows(at: insertions.map({ IndexPath(row: $0, section: 1) }),
                                       with: .automatic)
            self?.tableView.deleteRows(at: deletions.map({ IndexPath(row: $0, section: 1)}),
                                       with: .automatic)
            self?.tableView.reloadRows(at: modifications.map({ IndexPath(row: $0, section: 1) }),
                                       with: .automatic)
            self?.tableView.endUpdates()
        }
    })
}