Skip to content

Repository files navigation

AsyncView

Latest releaseSwift 5.5Platforms: iOS, macOS, tvOS, watchOS

A SwiftUI view that performs an async task when appearing and allows the caller show different views based on the success and failure cases.

Usage

This can be used with an async function that fetches some data and decodes it into a model. In the following example, we display a list of posts when they are received or show the error message if the call to fetchPosts fails.

structPost:Identifiable{letid:Stringlettitle:String}structPostsView:View{privatefunc fetchPosts()asyncthrows->[Post]{return[] // Actually implement network call!
}varbody:someView{AsyncView{tryawaitfetchPosts()} initial:{ProgressView()} success:{ posts inSuccess(posts: posts)} failure:{ error inFailure(error: error)}}structSuccess:View{letposts:[Post]varbody:someView{List(posts){ post inText(post.title)}}}structFailure:View{leterror:Errorvarbody:someView{Text(error.localizedDescription)}}}

About

A SwiftUI view that performs an async task on appearing, allowing the caller to display different views for the success and failure cases.

Topics

Resources

Stars

5 stars

Watchers

1 watching

Forks

Releases

Contributors

Languages