Carga diferida de contenido pesado
Posponga el trabajo costoso hasta que sea visible.
Carga diferida de contenido pesado es una lección gratuita de SwiftUI Academy en CoddyKit. Esta es la lección 3 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de SwiftUI Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de SwiftUI Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
Build Only What Is Visible
Rendering a thousand off-screen rows is wasteful. Lazy containers build views only as they scroll into view, saving huge amounts of work.
Eager vs Lazy Stacks
A plain VStack builds every child up front. Inside a ScrollView with many items, that startup cost can freeze the screen.
Reach for LazyVStack
Swap in LazyVStack inside a ScrollView. It defers creating each row until that row is about to appear on screen.
ScrollView {
LazyVStack {
ForEach(items) { RowView(item: $0) }
}
}List Is Lazy Too
A List recycles rows automatically, much like a lazy stack. For long, simple feeds it is often the easiest fast choice.
Lazy Grids for Cards
For galleries, use LazyVGrid. It lays out cards in columns and only builds the cells currently in the viewport.
LazyVGrid(columns: cols) {
ForEach(photos) { Thumb(photo: $0) }
}Load Images Asynchronously
Heavy images should not block the main thread. AsyncImage fetches and decodes off-screen, showing a placeholder until ready.
AsyncImage(url: photo.url) { img in
img.resizable()
} placeholder: { ProgressView() }Downsample Big Images
A full-resolution photo in a small cell wastes memory. Downsample to the displayed size so scrolling stays smooth.
Defer Expensive Work
Compute heavy values when a row first appears, not for all rows at once. The onAppear hook lets you start that work just in time.
RowView(item: item)
.onAppear { item.prefetch() }Paginate Long Data
Do not load ten thousand records at once. Paginate: fetch the next page as the user nears the bottom of the list.
Lazy Has a Trade-off
Lazy views forget off-screen content, so building it again on scroll-back costs work. Keep each cell's body light to stay smooth.
Combine the Techniques
Real apps stack these: lazy containers, async images, and pagination together keep memory low and frames steady.
Quick Check
What is the main benefit of LazyVStack over VStack in a long scroll?
Recap: Lazy Loading
You learned to defer work with lazy containers, load images asynchronously, and paginate data so heavy content scrolls without stutter. 🚀
Preguntas frecuentes
¿La lección «Carga diferida de contenido pesado» es gratis?
Sí — el texto completo de «Carga diferida de contenido pesado» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de SwiftUI Academy, actualiza a CoddyKit PRO. El curso de SwiftUI Academy incluye 4 lecciones en total.
¿Qué aprenderé en «Carga diferida de contenido pesado»?
Posponga el trabajo costoso hasta que sea visible. Practicas SwiftUI Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar SwiftUI Academy?
No se requiere experiencia previa. SwiftUI Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 3 de 4.
¿Cuánto tiempo toma la lección «Carga diferida de contenido pesado»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de SwiftUI Academy?
Sí. Cada lección de SwiftUI Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Cómo SwiftUI recalcula las vistas
- Evitar redibujos innecesarios
- Carga diferida de contenido pesado
- Analizar el rendimiento con Instruments