Загрузка видео с помощью Alamofire останавливает интерфейс в Swift iOS - PullRequest
0 голосов
/ 02 июля 2019

Я занимаюсь разработкой приложения для редактирования видео в Swift. Я загружаю видео клипы с прогрессом.

Моя проблема в том, что когда я загружаю видео с помощью Alamofire, это останавливает мой пользовательский интерфейс, пока процесс не завершится. Вот мой код:

    //MARK: -  Download Video file
    func downloadVideoFileFromUrl(videoUrl: URL, video: VideoFileModel) {

        var uniqueVideoID = ""
        var uniqueID = ""

        uniqueID = video.fileID

        uniqueVideoID = uniqueID  + ".MOV"

        let destination: DownloadRequest.DownloadFileDestination = { _, _ in
            var documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]

            // the name of the file here I kept is yourFileName with appended extension
            documentsURL.appendPathComponent(uniqueVideoID)
            return (documentsURL, [.removePreviousFile])
        }

        var vidProgress: Float = 0.0


        let manager = Alamofire.SessionManager.default
        manager.session.configuration.timeoutIntervalForRequest = 900
        manager.session.configuration.timeoutIntervalForResource = 900

        manager.download(videoUrl, to:destination)
            .downloadProgress { (progress) in

                DispatchQueue.main.async(execute: {

                    vidProgress = Float(progress.fractionCompleted)
                    video.downloadFromUrlProgress = vidProgress

                    //post notification
                    let userInfo = [ "videoFileModel" : video]
                    NotificationCenter.default.post(name: Notification.Name(rawValue: "VideoDownloadProgress"), object: nil, userInfo: userInfo)

                })

            }
            .response { defaultDownloadResponse in

                DispatchQueue.main.async(execute: {

                    print(defaultDownloadResponse.destinationURL as Any)

                    if defaultDownloadResponse.destinationURL != nil{
                        video.localFilePath = (defaultDownloadResponse.destinationURL?.absoluteString)!
                        video.downloadFromUrlProgress = 1
                        video.isVideoDownload = true

                        let userInfo = [ "videoFileModel" : video]
                        NotificationCenter.default.post(name: Notification.Name(rawValue: "VideoDownloadProgress"), object: nil, userInfo: userInfo)
                        print("Completed!")

                    }

                })
        }
    }

Я вызываю этот метод, используя

DispatchQueue.global(qos: .userInitiated).sync { }

Может кто-нибудь помочь мне, как я могу решить эту проблему?

Ответы [ 3 ]

0 голосов
/ 02 июля 2019

Попробуйте использовать следующий код:

func download(url: String,fileName: String,progressUpdate: ((_ percent: Double) -> Void)? = nil, completion:@escaping (_ success: Bool, _ error: Error?,_ fileUrl: URL?) -> Void) {
        let utilityQueue = DispatchQueue.global(qos: .utility)
        Alamofire.download(url)
            .downloadProgress(queue: utilityQueue) { progress in
                DispatchQueue.main.async {
                    progressUpdate?(progress.fractionCompleted)
                }
            }
            .responseData { response in
                if let data = response.result.value {
                    let fileURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent(fileName)
                    do {
                        try data.write(to: fileURL, options: .atomic)
                        completion(true,nil,fileURL)
                    } catch {
                        completion(false,error,nil)
                    }
                }
        }

    }

Как это использовать:

download(url: "", fileName: "filName", progressUpdate: { (progress) in
            print("Progress \(progress)")
        }) { (success, error, filePath) in
            print("success \(success) error \(error?.localizedDescription ?? "nil") path \(filePath?.absoluteString ?? "nil")")
        }
0 голосов
/ 02 июля 2019

вам может понадобиться проверить дельту прогресса, если прогресс изменяется менее чем на 1%, просто верните его и проигнорируйте вместо вызова dispatch.async

0 голосов
/ 02 июля 2019

Вместо

DispatchQueue.global(qos: .userInitiated).sync { }

попробовать

DispatchQueue.global(qos: .userInitiated).async { }

Похоже, вы пытаетесь загрузить видео как синхронизированную операцию

...