With Xcode 14.3, if I try to call loadItem on NSItemProvider, I get these warnings:
Non-sendable type '[AnyHashable : Any]?' exiting main actor-isolated context in call to non-isolated instance method 'loadItem(forTypeIdentifier:options:)' cannot cross actor boundary
Non-sendable type 'any NSSecureCoding' returned by call from main actor-isolated context to non-isolated instance method 'loadItem(forTypeIdentifier:options:)' cannot cross actor boundary
But I don't control the loadItem function or NSSecureCoding class. How can I avoid the sendable warnings?
Update: Interestingly if I take this function out of a UIViewController subclass and keep it out of classes that use @MainActor I do not get the warning.
John
func loadItem() async throws {
var attachment: NSItemProvider!
let loadedAttachment = try await attachment.loadItem(forTypeIdentifier: UTType.propertyList.identifier)
print("attachment: \(String(describing: attachment)), loadedAttachment: \(String(describing: loadedAttachment))")
}
1 Like
grigorye
(Grigory Entin)
2
As far as I got it, the warning about [AnyHashanble: Any]? comes from options argument in loadItem.
Eventually, I got it worked around by switching to loadData as defined by the following extension (tailored towards UTType and Data). Using it does not produce any warning.
extension NSItemProvider {
func loadData(for type: UTType) async throws -> Data {
guard let data = try await loadItem(forTypeIdentifier: type.identifier/*, options: nil*/) as? Data else {
enum Error: Swift.Error {
case dataIsNotExtractable(UTType)
}
throw Error.dataIsNotExtractable(type)
}
return data
}
}