I am doing things, I think, by the book. However, I keep running into compiler warnings/errors.
Here’s the affected code, where currentMediaTitle
is just a String
, and player
is just a AVPlayer
:
currentMediaTitle = ""
Task {
guard let asset = player.currentItem?.asset else {
currentMediaTitle = nil
return
}
let allMetadata = try await asset.load(.metadata) // WARNING: Non-sendable type '[AVMetadataItem]' returned by implicitly asynchronous call to nonisolated function cannot cross actor boundary
guard let titleMetadata = allMetadata.first(where: { item in
item.identifier == AVMetadataIdentifier.commonIdentifierTitle
})
else {
currentMediaTitle = nil
return
}
let titleValue = try await titleMetadata.load(.value) // WARNING: Passing argument of non-sendable type 'AVMetadataItem' outside of main actor-isolated context may introduce data races
let title = (titleValue as? NSString) as String?
self.currentMediaTitle = title
}
here’s a screenshot version if that’s better for you:
So there’s a few things that… feel paradoxical to me:
- The old ways of using things like
.metadata
are deprecated in favor of this new structured concurrency.load(.metadata)
method, but at the same time the value returned from this new method then also breaks the contracts of structured concurrency - It seems to be treating
AVAsset
andorAVMetadataItem
asactor
s for some reason, when they’re bothclass
es - It says I’m passing an argument of type
AVMetadataItem
, but I’m calling a method on it. Maybe I can understand how it thinks this because Swift seems to treat methods as static functions which take theirself
as their first argument, but even then I don’t see how this could possibly break structured concurrency since it’s self-scoped
I even tried a few things to get this all to happen on the MainActor
but it still gave me the exact same warnings so like.. I’m kinda stuck here.
What do I do to resolve these warnings?