Async is a easily handle async state and simple state management library for SwiftUI
.
In the Async philosophy the asynchronous state is seen as divided into three parts. One is success
. The other is failure
. Finally, the loading
state before execute async function.
Async is provided two way for async state management named @Async
property wrapper and AsyncView
. These two structures are used separately and simplify the division of the three states. They essentially have the same function, but can be used in different usecase.
- Use
@Async
. First, Define@Async
with state type. The code example below refers to aString
. Next,async
call as function with asynchronous function. The code example below refers to arun
function and can chain to access async state for switching decide view for each async state. This method also allows access toasync.state
and the use ofalert(isPresented:error:actions)
modifier withasync.error
. SeeExample
for more information.
struct ContentView: View {
@Async<String, Error> var async
var body: some View {
switch async(run).state {
case .success(let value):
Text("\(value)")
case .failure(let error):
Text(error.localizedDescription)
case .loading:
ProgressView()
}
}
}
- Use
AsyncView
. Pass async function directly toAsyncView
initializer, and define three states view viawhen
.
struct ContentView: View {
var body: some View {
AsyncView(run, when: (
success: { Text("\($0)") },
failure: { Text($0.localizedDescription) },
loading: { ProgressView() }
))
}
}
Async is released under the MIT license. See LICENSE for details.