swift-task-store is a Swift 6 library providing TaskStore, an observable container for managing keyed async tasks with configurable duplicate-key handling.
- Task Management in Swift Part 1: The Problem
- Task Management in Swift Part 2: Introducing the TaskStore
- Task Management in Swift Part 3: Duplicate Key Behavior
swift build # Build the library
swift test # Run all testsTaskStore<Key>- Main@Observableclass managing tasks by keyTaskStoreDuplicateKeyBehavior- Value type configuring how duplicate keys are handledTaskNameProvider- Protocol for customizable task naming (SwiftUI-style pattern)- Built-in:
.keyDescription,.constant("name"),.fromKey { ... } - Modifiers:
.withPrefix("prefix"),.withIncrementingSuffix()
- Built-in:
- Three task creation methods:
addConcurrentTask- Runs on global concurrent executor (but still propagates TaskLocal values, unlikeTask.detached)addIsolatedTask- Inherits caller's actor isolation (likeTask.init)addImmediateTask- UsesTask.immediate(Swift 6.2+, SE-0472) to run synchronously until first suspension point
- Uses
isolation: isolated (any Actor)? = #isolationparameter to capture caller's actor context for thread safety - Internal
TaskDatastruct pairs tasks with IDs to prevent stale task cleanup when tasks share keys - Cannot conform to
Sendabledue to@Observablemutable state; isolation is handled via theisolationparameter instead
Tests use Swift Testing framework with:
actor TestState<T>for thread-safe mutable state in testsactor OrderTrackerfor tracking event orderingAsyncStreamfor synchronizing task start/finish signals
Avoid using confirmation() for tasks that start asynchronously without awaiting inside the confirmation block.
- Empty line after imports
- Empty lines should have no whitespace
- Never force unwrap or force try