Skip to content
Back to skills

Swift Concurrency

ASecurity

Swift’s concurrency model uses `async` to mark asynchronous functions and `await` when calling them. ```swift func fetchUsername() async throws -> String { let (data, _) = try await URLSession.shared.data( from: URL(string: "https://example.com/user")! ) return String(decoding: data, as: UTF8.self) } func loadUser() { Task { do { let username = try await fetchUsername() print(username) } catch is CancellationError { print("Operation cancelled") } catch { print("Request failed:", error) } } } ...

  • 584 stars
  • 0 votes
  • 0 copies
  • 3 views
  • Added September 5, 2026
developmentswiftdatabase

Security analysis

A100/100

Pro scans all 16 files and shows the line behind each finding

Scanned September 5, 2026

npx -y skills add HoangNguyen0403/agent-skills-standard --skill swift-concurrency --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Swift Concurrency?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Swift Concurrency
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/hoangnguyen0403-swift-concurrency-agent-skills-standard/badge)](https://www.skillsdirectory.com/skills/hoangnguyen0403-swift-concurrency-agent-skills-standard)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
Swift’s concurrency model uses `async` to mark asynchronous functions and `await` when calling them.

```swift
func fetchUsername() async throws -> String {
    let (data, _) = try await URLSession.shared.data(
        from: URL(string: "https://example.com/user")!
    )

    return String(decoding: data, as: UTF8.self)
}

func loadUser() {
    Task {
        do {
            let username = try await fetchUsername()
            print(username)
        } catch is CancellationError {
            print("Operation cancelled")
        } catch {
            print("Request failed:", error)
        }
    }
}
```

An `async` function can suspend without blocking its thread. `await` may only be used inside another `async` context, such as an async function or `Task`.

For independent operations, use `async let` to run them in parallel:

```swift
func loadDashboard() async throws -> (Profile, [Message]) {
    async let profile = fetchProfile()
    async let messages = fetchMessages()

    return try await (profile, messages)
}
```

For a dynamic number of tasks, use `withTaskGroup` or `withThrowingTaskGroup`:

```swift
func fetchAll(ids: [Int]) async throws -> [Profile] {
    try await withThrowingTaskGroup(of: Profile.self) { group in
        for id in ids {
            group.addTask {
                try await fetchProfile(id: id)
            }
        }

        var profiles: [Profile] = []
        for try await profile in group {
            profiles.append(profile)
        }

        return profiles
    }
}
```

Handle cancellation explicitly:

```swift
func processItems(_ items: [Item]) async throws {
    for item in items {
        try Task.checkCancellation()
        await process(item)
    }
}
```

You can also check `Task.isCancelled` in long-running loops. Propagate `CancellationError` unless cancellation is intentionally handled.

Use an `actor` to protect shared mutable state:

```swift
actor ImageCache {
    private var images: [URL: Data] = [:]

    func data(for url: URL) -> Data? {
        images[url]
    }

    func store(_ data: Data, for url: URL) {
        images[url] = data
    }

    nonisolated func cacheDescription() -> String {
        "Image cache"
    }
}
```

Actor-isolated methods are called with `await` from outside the actor:

```swift
let cache = ImageCache()

if let data = await cache.data(for: imageURL) {
    print(data.count)
}
```

Annotate UI views and view models with `@MainActor`:

```swift
@MainActor
final class UserViewModel {
    var username = ""

    func load() async {
        do {
            username = try await fetchUsername()
        } catch is CancellationError {
            return
        } catch {
            print(error)
        }
    }
}
```

For an inline UI update from another context, use:

```swift
await MainActor.run {
    label.text = "Loaded"
}
```

Use `@GlobalActor` when a resource needs a custom global isolation domain:

```swift
@globalActor
actor DatabaseActor {
    static let shared = DatabaseActor()
}
```

Create child work with `Task { ... }`; it inherits the surrounding task’s isolation and priority. Avoid `Task.detached` unless you explicitly need to break context inheritance. Keep synchronous work off `@MainActor`, and perform all UI updates on `@MainActor`.

Files in this skill

  • eval-1.baseline.md898 B
  • eval-1.with-skill.md3.2 KB
  • eval-2.baseline.md714 B
  • eval-2.with-skill.md1.5 KB
  • eval-3.baseline.md929 B
  • eval-3.with-skill.md1.4 KB
  • eval-4.baseline.md773 B
  • eval-4.with-skill.md1.4 KB
  • eval-5.baseline.md1.7 KB
  • eval-5.with-skill.md2.1 KB
  • trigger-1.md120 B
  • trigger-2.md120 B
  • trigger-3.md135 B
  • trigger-4.md156 B
  • trigger-5.md122 B

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.