Get rid of warning: Passing argument of non-sendable type ‘AVAssetExportSession’ outside of main actor-isolated context may introduce data races

How can I get rid of this warning for await exportSession.export():

Code:

func extractAudioFromImportedVideo(videoURL: URL, success: @escaping ((URL) -> Void), failure: @escaping ((Error?) -> Void)) async {
        
        do {
            let asset = AVURLAsset(url: videoURL)
            let audioTracks = try await asset.loadTracks(withMediaType: .audio)
            
            guard !audioTracks.isEmpty else {
                failure(NSError(domain: "Audio Extraction Error", code: 0, userInfo: [NSLocalizedDescriptionKey: "Video does not contain audio"]))
                return
            }
            
            guard let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetAppleM4A) else {
                failure(NSError(domain: "Audio Extraction Error", code: 0, userInfo: [NSLocalizedDescriptionKey: "Failed to create export session"]))
                return
            }
            
            guard let audioURL = makeFileOutputURL(fileName: "ExtractedAudio.m4a") else {
                failure(NSError(domain: "Audio Extraction Error", code: 0, userInfo: [NSLocalizedDescriptionKey: "Failed to create output URL"]))
                return
            }
            
            print("audioTracks", audioTracks)
            exportSession.outputURL = audioURL
            exportSession.outputFileType = .m4a

            
            await exportSession.export()
            print("exportSession.status", exportSession.status.rawValue)
            if exportSession.status == .completed {
                // Audio extraction succeeded
                print("Audio extraction succeeded")
                success(audioURL)
            } else {
                // Audio extraction failed
                print("Audio extraction failed: (exportSession.error?.localizedDescription ?? "")")
                failure(exportSession.error)
            }
        }
        catch {
            print("extractAudioFromImportedVideo Error:", error.localizedDescription)
        }
    }

1

The error is:

Passing argument of non-sendable type ‘AVAssetExportSession’ outside of main actor-isolated context may introduce data races; this is an error in the Swift 6 language mode

This is telling you that the problem is arising because you are performing this in a main actor-isolated function.

The easy fix is to remove this actor-isolation, to make the function nonisolated:

nonisolated func extractAudioFromImportedVideo(videoURL: URL, success: @escaping ((URL) -> Void), failure: @escaping ((Error?) -> Void)) async {
    …
}

Personally, I would eliminate these closures and just follow async patterns:

nonisolated func extractAudioFromImportedVideo(videoURL: URL) async throws -> URL {
    do {
        let asset = AVURLAsset(url: videoURL)
        let audioTracks = try await asset.loadTracks(withMediaType: .audio)

        guard !audioTracks.isEmpty else {
            throw AudioExtractionError.noAudio
        }

        guard let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetAppleM4A) else {
            throw AudioExtractionError.exportFailed
        }

        guard let audioURL = makeFileOutputURL(fileName: "ExtractedAudio.m4a") else {
            throw AudioExtractionError.outputUrlFailed
        }

        print("audioTracks", audioTracks)
        exportSession.outputURL = audioURL
        exportSession.outputFileType = .m4a


        await exportSession.export()
        print("exportSession.status", exportSession.status.rawValue)
        if let error = exportSession.error {
            throw error
        }

        print("Audio extraction succeeded")
        return audioURL
    } catch {
        print("extractAudioFromImportedVideo Error:", error.localizedDescription)
        throw error
    }
}

Note, I also retired NSError, replacing it with:

enum AudioExtractionError: LocalizedError {
    case noAudio
    case exportFailed
    case outputUrlFailed

    nonisolated var errorDescription: String? {
        return switch self {
        case .noAudio: String(localized: "Video does not contain audio", comment: "AudioExtractionError")
        case .exportFailed: String(localized: "Failed to create export session", comment: "AudioExtractionError")
        case .outputUrlFailed: String(localized: "Failed to create output URL", comment: "AudioExtractionError")
        }
    }
}

That is actually localized, isolates the details from this function, and enumerated errors support richer error handling.


FWIW, once you have this working, you can simplify further:

nonisolated func extractAudioFromImportedVideo(videoURL: URL) async throws -> URL {
    let asset = AVURLAsset(url: videoURL)
    let audioTracks = try await asset.loadTracks(withMediaType: .audio)

    guard !audioTracks.isEmpty else {
        throw AudioExtractionError.noAudio
    }

    guard let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetAppleM4A) else {
        throw AudioExtractionError.exportFailed
    }

    guard let audioURL = makeFileOutputURL(fileName: "ExtractedAudio.m4a") else {
        throw AudioExtractionError.outputUrlFailed
    }

    exportSession.outputURL = audioURL
    exportSession.outputFileType = .m4a

    await exportSession.export()
    if let error = exportSession.error {
        throw error
    }

    return audioURL
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật