TaskCancelKit is a Swift library designed to simplify the management of cancellable tasks, particularly useful for asynchronous operations in iOS and macOS applications. With TaskCancelKit, developers can easily cancel ongoing tasks, preventing unwanted side effects and managing resources efficiently.
Easy Task Management: Cancel individual or all tasks with simple method calls. Type-Safe Task Identifiers: Utilize Swift's type system to manage tasks with custom identifiers. Lightweight and Efficient: A minimalistic approach that's easy to integrate into any project.
- iOS 13.0+ / macOS 10.15+
- Swift 5.3+
You can add TaskCancelKit to your project via Swift Package Manager by adding the following to your Package.swift file:
dependencies: [
.package(url: "https://github.com/yourusername/TaskCancelKit.git", .upToNextMajor(from: "1.0.0"))
]
Replace https://github.com/yourusername/TaskCancelKit.git with the actual URL of your repository.
First, create an instance of TaskCancelBag where you can store your tasks.
import TaskCancelKit
private let cancelBag = TaskCancelBag<TaskID>()
When you create a task, store it in your cancelBag using an identifier.
enum TaskID {
case countThree
}
func executeAfterThreeSecond() {
Task {
try await Task.sleep(for: .seconds(3))
print("Task Complete")
}.store(in: cancelBag, id: .countThree)
}
You can cancel tasks individually or all at once.
// Cancel a specific task
cancelBag.cancel(id: .countThree)
// Cancel all tasks
cancelBag.cancelAll()
(Include any advanced features or usage instructions here.)
We welcome contributions to TaskCancelKit! Please read our Contributing Guide for details on how to propose bugfixes, features, and submit pull requests.
TaskCancelKit is released under the MIT license. See LICENSE for more information.