我有一个列表,它会在接近末尾的时候自动获取更多数据:
struct AdCardListView: View {
@ObservedObject var model: AdListViewModel = AdListViewModel()
var body: some View {
List { ForEach(self.model.adArray.enumerated().map({ $0 }), id: \.element.id) { index, ad in
AdCardView(ad: ad)
.onAppear {
DispatchQueue.main.async {
let count = self.model.adArray.count
if index >= count - 5 { //5 Views to the end, start loading more.
self.model.fetch(count: count)
}
}
}
}
}
}
}模型看起来像:
final class AdListViewModel: ObservableObject {
@Published var adArray = [Ad]()
...
func fetch(count: Int = 0) {
...
DispatchQueue.main.async {
self.adArray.append(contentsOf: newAds) //<-- problem here
}
...
}我的问题是:每次修改@Published/@ObservedObject时,我在滚动列表时都会有一点冻结。另外,我发现List会重新计算所有可见视图+上下几个视图的正文。
但我不能确定是什么导致滚动挂起并修复它(也许freeze是一个距离等于(滚动速度*渲染时间)的传输?
为什么SwiftUI在现有视图上重新计算主体?他们没有改变!
你能帮帮我吗?
发布于 2020-02-18 21:30:51
身体的重建应该不是问题,我怀疑性能问题是否与列表的更新动画有关。
您是否尝试添加.id(UUID())修改器到您的列表,以便动画被丢弃,并在更新后重新创建列表。
https://stackoverflow.com/questions/60045110
复制相似问题