loadItem on NSItemProvider - how to avoid Sendable warnings

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

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
    }
}

I am not sure if this was a bug in previous compiler versions, but the extension does not work for me in Xcode 16 beta 6.

Still getting the original warnings @johnbrayton reported.

Trying to use NSItemProvider from within an iOS share extension.

1 Like

Me too. Just tried. Any luck?