0Pricing
SwiftUI Academy · Aula

Deslize para atualizar

Recarregue os dados com o modificador refreshable.

Deslize para atualizar é uma aula grátis de SwiftUI Academy no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de SwiftUI Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de SwiftUI Academy inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

Refreshing on Demand

Users expect to pull a list down to reload it. SwiftUI bakes this gesture in with a single modifier, no gesture code required. ✨

Meet refreshable

Attach the refreshable modifier to a List. Pulling down reveals a spinner and runs the async work you provide.

.refreshable {
    await reload()
}

It Expects Async Work

The refreshable closure is async, so it pairs perfectly with await and modern Swift concurrency for fetching data.

Spinner Tied to Your Task

The pull-to-refresh spinner stays visible until your await finishes. SwiftUI hides it the moment the work completes.

A Simple Reload

Inside refreshable, call an async function that fetches fresh data and updates your @State array on completion.

func reload() async {
    items = await fetchItems()
}

Awaiting a Network Call

A real reload usually awaits a network request. Use URLSession with async/await to pull new items from a server.

let (data, _) = try await
    URLSession.shared.data(from: url)

Update State After Await

Once the await returns, assign the decoded results to your @State array and the list refreshes with the new rows.

let fresh = try decoder.decode(
    [Item].self, from: data)
items = fresh

Handle Errors Gracefully

Network calls can fail. Wrap the work in do/catch so a dropped connection shows a message instead of crashing.

do {
    items = try await fetchItems()
} catch {
    showError = true
}

Refreshable Needs a List

The pull gesture only works on scrollable containers like List or ScrollView. Attaching refreshable to a VStack does nothing.

Keep It Quick

Refresh should feel snappy. If your fetch is slow, show progress and avoid blocking the main thread with heavy work.

Pairs With Other Editing

Pull-to-refresh sits happily beside onDelete, onMove, and add. Together they make a fully interactive, editable list. ✨

Quick Check

What kind of closure does refreshable take?

Recap: Pull-to-Refresh

You added reload-on-pull with the async refreshable modifier on a List, awaiting fresh data and handling errors. 🎉

Perguntas Frequentes

A aula “Deslize para atualizar” é grátis?

Sim — o texto completo de “Deslize para atualizar” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de SwiftUI Academy, atualize para CoddyKit PRO. O curso de SwiftUI Academy inclui 4 aulas no total.

O que vou aprender em “Deslize para atualizar”?

Recarregue os dados com o modificador refreshable. Você pratica SwiftUI Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar SwiftUI Academy?

Nenhuma experiência prévia é necessária. SwiftUI Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.

Quanto tempo leva a aula “Deslize para atualizar”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de SwiftUI Academy?

Sim. Cada aula de SwiftUI Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Linhas com deslize para excluir
  2. Reordenando com onMove
  3. Adicionando novos itens
  4. Deslize para atualizar
← Voltar para SwiftUI Academy