Загрузка Alamofire в macOS. Выглядит нормально, прогресс работает, но файла нет?

#swift #alamofire

#swift #alamofire

Вопрос:

Я добавил Alamofire через cocoapods, и у меня есть способ загрузить zip-файл (около 50 МБ).

Во время загрузки все выглядит идеально. Я вижу в Activity Monitor, что для моего приложения загружено 50 МБ, я вижу, как бегает строка прогресса. Но я никогда не могу найти файл.

Прямо сейчас я настроил его на использование текущего каталога, но на всякий случай попробовал другие. Я даже обыскал весь диск по измененным данным и ничего не нашел.

Вот мой код.

     func downloadAndInstall(){
        log.info("Downloading and Installing.....")
        displayToUser(content: "Downloading and Installing.....")

        let urlString = updatePackageURL //(This is http://xxxx.com/xxxxpackage.zip)
        let fileManager = FileManager.default
        currentDir = fileManager.currentDirectoryPath
        let fileURL: URL = URL(string: currentDir   "/package.zip")!
        let destination: DownloadRequest.DownloadFileDestination = { _, _ in (fileURL, []) }
        log.info("FILEURL: (fileURL)")
        var progressValues: [Double] = []
        var response: DefaultDownloadResponse?

        Alamofire.download(urlString, to: destination)
            .downloadProgress { progress in
                progressValues.append(progress.fractionCompleted)
                log.info("Latest Progress Value: (progress.fractionCompleted)")
                self.progBar.doubleValue = progress.fractionCompleted
            }
            .response { resp in
                response = resp
                if progressValues.last != 1.0 {
                    //backout of the process, something went wrong
                    log.debug("Something went wrong downloading the file.  Close and try again.")
                    self.displayToUser(content: "Something went wrong downloading the file. Close and try again.")
                    self.exitpoorly()
                }
                else{
                    log.info("Download Finished")
                    self.displayToUser(content: "Download Finished")
                    self.extractpackage()
                }
        }

        var previousProgress: Double = progressValues.first ?? 0.0

        for progress in progressValues {
            previousProgress = progress
        }

        if let lastProgressValue = progressValues.last {
            log.info("Current Download Value: (lastProgressValue, 1.0)")
        } else {
            //Fail
        }
    }
  

Ответ №1:

Я бы посоветовал проверить наличие каких-либо ошибок, например:

 Alamofire.download(urlString, to: destination)
    .downloadProgress { progress in
        ...
    }
    .response { response in
        guard response.error == nil else {
            //backout of the process, something went wrong
            log.debug("Something went wrong downloading the file.  Close and try again.")
            log.debug(response.error!.localizedDescription)
            ...
            self.exitpoorly()
            return
        }

        log.info("Download Finished")
        ...
}
  

Возможно, приложение изолировано или, возможно, у вас нет разрешений для этой папки. Трудно сказать, не видя ошибки.

Комментарии:

1. Оказывается, у меня проблема с URL-адресом файла… Сказал мне именно то, что мне нужно было увидеть. Спасибо!