Question:
I'm working on a project in Xcode 16.1, using Swift 6 with iOS 18. My code is working fine in Swift 5, but I'm running into concurrency issues when upgrading to Swift 6, particularly with the @preconcurrency attribute in AVFoundation.
Here is the relevant part of my code:
import SwiftUI
@preconcurrency import AVFoundation
struct OverlayButtonBar: View {
...
let audioTracks = await loadTracks(asset: asset, mediaType: .audio)
...
// Tracks are extracted before crossing concurrency boundaries
private func loadTracks(asset: AVAsset, mediaType: AVMediaType) async -> [AVAssetTrack] {
do {
return try await asset.load(.tracks).filter { $0.mediaType == mediaType }
} catch {
print("Error loading tracks: \(error)")
return []
}
}
}
Issues:
When using @preconcurrency,
I get the warning:
@preconcurrency
attribute on module AVFoundation
has no effect. Suggested fix by Xcode is: Remove @preconcurrency
.
But if I remove @preconcurrency,
I get both a warning and an error:
- Warning: Add '@preconcurrency' to treat 'Sendable'-related errors from module 'AVFoundation' as warnings.
- Error: Non-sendable type
[AVAssetTrack]
returned by implicitly asynchronous call to nonisolated function cannot cross actor boundary. (ClassAVAssetTrack
does not conform to theSendable
protocol(AVFoundation.AVAssetTrack)).
This error comes if I attempt to directly access non-Sendable AVAssetTrack in an async context :
let audioTracks = await loadTracks(asset: asset, mediaType: .audio)
How can I resolve this issue while staying compliant with Swift 6 concurrency rules? Is there a recommended approach to handling non-Sendable types like AVAssetTrack
in concurrency contexts?
Appreciate any guidance on making this work in Swift 6, especially considering it worked fine in Swift 5.
Thanks in advance!
Leave the @preconcurrency
attribute in and ignore the warning (for now). I heard it is a bug in some version if the compiler betas.
Have you tried the same with Xcode 16.0 Release Candidate (Sep 9th)? It should aready have a newer compiler than Xcode 16.1 (Aug 12th).