Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

swift - SwiftUI List updating slowly

I have a SwiftUI List, which has thousands of items. Each item is as such:

class Item: Codable, Identifiable {
    var id = UUID()
    var prop1 = ""
    var prop2 = ""
}

Now I do this:

List(store.items.indices, id: .self) { index in
    DetailView(item: self.$store.items[index])
}.id(UUID())

I iterate the indicies because I have to pass a Binding to the DetailView.

Now the DetailView has TextFields attached to the Item's properties.

When DetailView changes an Item's properties, it causes the List to refresh ALL of the items, which I dont want.

Also, it's still important that when one of those properties changes, the main view is updated, because on macOS, the main view is still visible even when you click on one of the items.

My question is how to make it so that whenever an Item passed in from the List changes, only update that row?

I also tried a solution mentioned where I use a LazyVStack, and it fixes the performance issue, but I use the List inside of a NavigationView, and the LazyVStack doesn't seem to support that.

Any suggestions are greatly appreciated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Remove .id(UUID()) from the List.

By default, List diffs changes and only updates the affected rows. For very large data sets, that .id(UUID()) trick is used to force the whole List to reload when something changes, which may be faster than trying to diff the large dataset. It doesn’t sound like you need it, so it’s actually worse to include it unnecessarily.

Edit: I don’t think binding to the array by index is the problem, but it is unusual and may bite you eventually.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.7k users

...