Sometimes, the better option is to be lazy.

Alvar Arias
2 min readJun 25, 2023

--

When you need to load a big long list of data inside of your view, the best option is to use LazyVStack.

When you are programming in SwiftUI and need to load a large list of data within your view, the best option is to use LazyVStack.

So, what is LazyVStack in SwiftUI? According to Apple documentation, it is “ a view that arranges its children in a vertically growing line, creating items only as needed ”.

The stack is ‘lazy’ in the sense that it doesn’t create items until they need to be rendered onscreen. This becomes crucial when dealing with scenarios like reading a JSON file containing information on more than 3000 customers, which needs to be displayed in the main view alongside two other different views.

ForEach(items, id: \.self) { index in

LazyVStack{

LazyHStack {

Text(product[index].name)

}

}
}

Upon analyzing my app’s performance, I noticed an increase in processor usage when opening the main view. To address this, I decided to implement `LazyVStack`, which significantly improved my app’s performance. It proved to be a quick and effective solution.

Additionally, I optimized the process by reading the JSON file only when the user first opens the view and then storing the data in a variable, eliminating the need to read the file every time the view is opened again.

.onAppear {
if theProducts.isEmpty {
loadProducts()
}
}

These changes allowed my app to load the data more efficiently and quickly, greatly enhancing the user experience of my product.

I hope this post proves to be useful for you. And remember, sometimes taking advantage of “ Lazy ” programming concepts can be the best option.

References:

--

--

Alvar Arias
0 Followers

IOS Developer, my mantra is please keep it simple and then build big.