Skip to content

A Swift package providing tools and types for managing asynchronous tasks. This package is designed to simplify the handling of cancellable asynchronous operations in SwiftUI applications by offering reusable view models and patterns. Async task swift and swiftui An asynchronous programming library for Swift Task manager Task kit

License

Notifications You must be signed in to change notification settings

panOpenAPI/async-task

 
 

Repository files navigation

Async task kit

Please star the repository if you believe continuing the development of this package is worthwhile. This will help me understand which package deserves more effort.

It's a start of the Swift package providing tools and types for managing asynchronous tasks. This package is designed to simplify the handling of cancellable asynchronous operations in SwiftUI applications by offering reusable view models and patterns.

SwiftUI example of using package

  1. async location
  2. replicate kit

Overview

AsyncTaskManager provides a set of tools for managing asynchronous tasks with support for:

  • Error handling: Handle errors gracefully with custom error handlers.
  • State management: Track the progress and result of tasks using reactive properties.
  • Task cancellation: Cancel tasks when they are no longer needed, freeing up resources.

Features

  • SingleTaskViewModel: A view model for managing a single cancellable asynchronous task.
  • Error-handling extensibility: Pass a custom error handler to adapt to your application's requirements.
  • Integration with SwiftUI: Leverage the @Published property wrapper for UI updates.

Usage

SingleTaskViewModel

The SingleTaskViewModel class manages a cancellable asynchronous operation. It tracks the operation's result, error, and activity state.

Properties

  • value: The result of the operation, if successful.
  • error: An error of type E if the operation fails.
  • isActive: Indicates whether the task is currently running.

Methods

  • start(operation:): Starts the asynchronous task.
  • cancel(): Cancels the currently running task.

Example

struct ExampleView: View {
    @StateObject private var viewModel = SingleTaskViewModel<String, Error>()
    
    var body: some View {
        VStack {
            if let value = viewModel.value {
                Text("Result: \(value)")
            } else if let error = viewModel.error {
                Text("Error: \(error.localizedDescription)")
            } else if viewModel.isActive {
                ProgressView("Loading...")
            } else {
                Button("Fetch Data") {
                    viewModel.start {
                        // Simulate an async task
                        try await Task.sleep(nanoseconds: 2 * 1_000_000_000)
                        return "Hello, World!"
                    }
                }
            }
        }
        .padding()
    }
}

About

A Swift package providing tools and types for managing asynchronous tasks. This package is designed to simplify the handling of cancellable asynchronous operations in SwiftUI applications by offering reusable view models and patterns. Async task swift and swiftui An asynchronous programming library for Swift Task manager Task kit

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Swift 100.0%