일련의 비디오를 다운로드하려면 Operation
및 OperationQueue
을 사용하고 있습니다. 내 다운로드는 Operation
입니다.작업 내에서 큐에 액세스
import UIKit
import AVFoundation
class VideoDownloader: Operation {
let videoRecord: VideoRecord
init(videoRecord: VideoRecord) {
self.videoRecord = videoRecord
}
override func main() {
if isCancelled {
return
}
let asset = AVURLAsset(url: videoRecord.url)
asset.resourceLoader.setDelegate(self, queue: DispatchQueue.main)
let exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHighestQuality)
let filename = "\(videoRecord.id).mp4"
let documentsDirectory = FileManager.default.urls(for: FileManager.SearchPathDirectory.documentDirectory, in: FileManager.SearchPathDomainMask.userDomainMask).last!
let outputURL = documentsDirectory.appendingPathComponent(filename)
exporter?.outputURL = outputURL
exporter?.outputFileType = .mp4
exporter?.exportAsynchronously {
if let error = exporter?.error {
print(error)
} else {
print(outputURL)
}
}
}
}
extension VideoDownloader: AVAssetResourceLoaderDelegate {
}
이 작동합니다. 그러나 나는 아래 라인에 대해 우려하고있다.
asset.resourceLoader.setDelegate(self, queue: DispatchQueue.main)
현재 주 스레드에서 대리인을 설정하고 있습니다. 이것은 백그라운드 스레드에서 실행되기 때문에 올바른 것으로 생각하지 않습니다. 나는 내가 틀린다면 나에게 정정 해주세요.
작업을 현재 실행중인 대기열에 대기열을 설정할 수 있다면 가장 좋을 것이라고 생각했습니다. 하지만 작업 클래스에서 대기열에 어떻게 액세스 할 수 있습니까?
원격 URL에서 비디오 파일을 다운로드 하시겠습니까? – SeanLintern88
@ SeanLintern88 네, 그렇습니다. – Isuru
TBH,이 같은 접근 방식을 본적이 없으며 일반적으로 UrlSessionDataTask를 사용하여 비디오를 다운로드 할 것입니다. AVAssetExportSession이 백그라운드에서 작동하지 않는다는 것을 알고 있습니다. 누군가가 앱을 배경으로 설정하면 실패합니다. – SeanLintern88