gpt4 book ai didi

swift - 将 UITableview 与订阅数据同步

转载 作者:行者123 更新时间:2023-11-30 10:43:40 24 4
gpt4 key购买 nike

我有一个Tableview,其操作有些复杂,其中通过 GraphQl 订阅实时插入和更新部分。

目前我在竞争条件方面遇到一些问题。当我通过订阅收到新数据时,我将其解析到我的模型中,并通过插入更新表格 View ,并更新某些现有单元格的内容和大小。

当我获取数据的速度快于完成表中之前的更新时,问题就会出现,导致“无效的部分数量”崩溃。

我认为解决方案是串行/等待序列订阅 -> 模型 -> TableView

我尝试让它与各种并发方法一起使用,例如信号量、同步、屏障、调度组。但一直未能成功弄清楚。

如果我尝试简化,事件的顺序就会像这样发生。

//型号

subscription { data in 

//should not start parsing new data until previous data has been drawn in table to avoid missmatch in model and table

parse(data)

}

func parse(data) {
//do stuff like update datamodel
figureOutWhatToUpdateInTable(data) { (insertSet, reloadSet) in
delegate.updateTableView(insertSet, reloadSet)
}
//do stuff
}

//VC

func updateTableView(insertSet, reloadSet) {

tableView.beginUpdates()
CATransaction.begin()

//once a new section is inserted we need to update content of some sections
CATransaction.setCompletionBlock {

reloadSet.forEach { (index: Int) in

let section = tableView.headerView(forSection: index)

section.updateData(data[index]) {
// call begin/end to make tableview get height
tableView.beginUpdates()
tableView.endUpdates()

// now im ready to parse new data into my model

}
})

}

tableView.insertSections(insetSet, with: .top)
CATransaction.commit()
tableView.endUpdates()

}

基本上我需要等待section.updateData完成,然后parse(data)处理来自订阅的任何新数据

最佳答案

在这种情况下您可以使用DispatchGroup

    let group = DispatchGroup()
group.enter()

// do some stuff (1)

group.leave()

group.notify(queue: .main) {
// do stuff after 1
}

调用group.leave()后,组自动在group.notify block 执行STUFF

关于swift - 将 UITableview 与订阅数据同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56276860/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com