SwiftUI?List在MacOS中的性能優(yōu)化示例
引言
List在iOS中有懶加載的特性,但是在MacOS中會一次性加載完List中的所有的數(shù)據(jù)。并沒有懶加載的特性。
所以在MacOS的List中當(dāng)數(shù)據(jù)量巨大時,會存在巨大的性能瓶頸。
var body: some View { List(){ ForEach(currentSectionModel) { (sectionModel) in Section(header: HStack { Text("section")+Text(sectionModel.word).font(.title).foregroundColor(.red) }.frame(height:35) ) { ForEach(currentSectionModel, id: \.self) { (wordModel) in Text(wordModel.word) } } } }
當(dāng)數(shù)據(jù)量達(dá)到15000條時, 在16寸i9的mbp上加載時長需要4.53s
這個時候建議使用 ScrollView + LazyVStack(macOS 11, iOS14支持)
ScrollView { LazyVStack { } }
來獲取巨大性能提升
var body: some View { ScrollView { LazyVStack { ForEach(currentSectionModel) { (sectionModel) in Section(header: HStack { Text("section")+Text(sectionModel.word).font(.title).foregroundColor(.red) }.frame(height:35) ) { ForEach(currentSectionModel, id: \.self) { (wordModel) in Text(wordModel.word) } } } } }.onAppear { DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) { currentSectionModel = storeData } } }
實(shí)測加載15000 條數(shù)據(jù)加載時長為31ms 加載時長為原來的 0.0068倍。 因為只加載了顯示的部分,所以性能提升巨大。
以上就是SwiftUI List在MacOS中的性能優(yōu)化示例的詳細(xì)內(nèi)容,更多關(guān)于SwiftUI List性能優(yōu)化MacOS的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Swift中優(yōu)雅處理閉包導(dǎo)致的循環(huán)引用詳解
這篇文章主要給大家介紹了關(guān)于Swift中優(yōu)雅的處理閉包導(dǎo)致的循環(huán)引用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Swift具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Swift中通過疊加UILabel實(shí)現(xiàn)混合進(jìn)度條的方法
這篇文章主要介紹了Swift中通過疊加UILabel實(shí)現(xiàn)混合進(jìn)度條的方法的相關(guān)資料,需要的朋友可以參考下2016-08-08