Skip to content

Compilation errors with Reference code related to Swift concurrency #8

Description

@inzan

Affected Skill

core-bluetooth

Reference File (if applicable)

skills/core-bluetooth/references/ble-patterns.md

What is incorrect?

Hi - thanks for sharing this skill. Just a small issue to report.

The CoreBluetooth reference code will not compile under Swift 6.2 with strict concurrency enabled. Passing non-sendable CoreBluetooth types across an actor hop is not allowed. For example below, a CBPeripheral is passed from nonisolated domain to MainActor. Even though underneath CoreBluetooth is using main queue, the compiler can't verify this so I end up getting "Sending 'peripheral' risks causing data races" wherever it passes a CB type into the Task block.

nonisolated func centralManager(
    _ central: CBCentralManager,
    didDiscover peripheral: CBPeripheral,
    advertisementData: [String: Any],
    rssi RSSI: NSNumber
) {
    Task { @MainActor in
        let name = peripheral.name ?? "Unknown"
        let device = DiscoveredDevice(
            id: peripheral.identifier,
            name: name,
            rssi: RSSI.intValue,
            peripheral: peripheral
        )
        if !discoveredDevices.contains(where: { $0.id == device.id }) {
            discoveredDevices.append(device)
        }
    }
}

If you'd like I could make a small PR to fix, I think the fastest way would just be to do something like

@preconcurrency import CoreBluetooth

Thanks again!

Apple Documentation References

https://developer.apple.com/documentation/Swift/Sendable

Key Section: The "Overview" section explicitly states: "You can safely pass values of a sendable type from one concurrency domain to another..." and details what the compiler requires for structures, actors, and classes to pass those checks.

Environment

XCode 26
Swift 6.2 with Strict Concurrency

Prompt or Context (optional)

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions