SwiftUI List在MacOS中的性能优化示例
<h2>引言</h2><p>List在iOS中有懒加载的特性,但是在MacOS中会一次性加载完List中的所有的数据。并没有懒加载的特性。</p>
<p>所以在MacOS的List中当数据量巨大时,会存在巨大的性能瓶颈。</p>
<div class="jb51code"><pre class="brush:cpp;">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)
}
}
}
}
</pre></div>
<p>当数据量达到15000条时, 在16寸i9的mbp上加载时长需要4.53s</p>
<p>这个时候建议使用 ScrollView + LazyVStack(macOS 11, iOS14支持)</p>
<div class="jb51code"><pre class="brush:cpp;">ScrollView {
LazyVStack {
}
}
</pre></div>
<p>来获取巨大性能提升</p>
<div class="jb51code"><pre class="brush:cpp;">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
}
}
}
</pre></div>
<p>实测加载15000 条数据加载时长为31ms 加载时长为原来的 0.0068倍。 因为只加载了显示的部分,所以性能提升巨大。</p>
<p>以上就是SwiftUI List在MacOS中的性能优化示例的详细内容,更多关于SwiftUI List性能优化MacOS的资料请关注琼殿技术社区其它相关文章!</p>
<div class="art_xg">
<b>您可能感兴趣的文章:</b><ul><li>swift中可选值?和!使用的方法示例</li><li>Swift中非可选的可选值类型处理方法详解</li><li>Swift Error重构优化详解</li><li>Swift使用SnapKit模仿Kingfisher第三方扩展优化</li><li>Swift中图片资源使用流程的优化方法详解</li><li>Swift可选值优化示例详解</li></ul>
</div>
</div>
<!--endmain-->
頁:
[1]