我有一个列表和一个数组中的大约 1000 个项目,并且我的项目实时更新(100 次更新/秒)。我的清单表现不佳。
这是我的测试代码(我尝试使用 List 和 LazyVStack):
struct PriceBoardView2 : View {
@EnvironmentObject var loadingEnv : LoadingEnv
@State var listUser : [UserModel]
var body: some View {
VStack(content: {
Button(action: {
updateData() //fake data realtime update
}, label: {
Text("Fake data realtime update")
})
List(content: {
ForEach(listUser.indices, id: \.self) { i in
RowPriceBoardView2(userModel: listUser[i])
}
})
// ScrollView(content: {
// LazyVStack(content: {
// ForEach(listUser.indices, id: \.self) { i in
// RowPriceBoardView2(userModel: listUser[i])
// }
// })
// })
})
.onAppear(perform: {
for i in 0..<1000 {
listUser.append(UserModel(number: i, name: "-", age: 0))
}
})
}
func updateData() {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.01, execute: {
let i = Int.random(in: 0..<1000)
self.listUser[i] = UserModel(number: i, name: "Pla pla", age: Int.random(in: 0..<1000))
updateData()
})
}
}
struct UserModel {
var number : Int
var name : String
var age : Int
}
struct RowPriceBoardView2: View {
var userModel : UserModel
var body: some View {
HStack(content: {
Text("\(userModel.number)")
Spacer()
Text("\(userModel.name)")
Spacer()
Text("\(userModel.age)")
})
.frame(width: .infinity, height: 30, alignment: .center)
}
}