如何执行alamofire后台上传请求?

编程入门 行业动态 更新时间:2024-10-22 17:16:39
本文介绍了如何执行alamofire后台上传请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要将zip文件发送到服务器端。

I need to send zip file to server side.

我有我需要在后台处理的请求

There is my request which I need to work in background

let configuration = URLSessionConfiguration.default configuration.timeoutIntervalForRequest = 10 // seconds alamoFireManager = Alamofire.SessionManager(configuration: configuration) appDeligate.log.debug("request was sended") alamoFireManager.upload(deligate.data!, to: deligate.url, method: .post, headers: headers) .uploadProgress(closure: { progress in print("Upload Progress: \(progress.fractionCompleted)") }) .validate() .responseJSON {}

现在可以正常使用了,但我需要在后台执行此操作。根据Alamofire自述文件

Now it is work properly, but I need to execute this in background. According to the Alamofire README doc

https:// github。 com / Alamofire / Alamofire

它说

具有后台配置的会话管理器

Creating a Session Manager with Background Configuration

让配置= URLSessionConfiguration.background(withIdentifier: com.example.app.background)

let configuration = URLSessionConfiguration.background(withIdentifier: "com.example.app.background")

让sessionManager = Alamofire.SessionManager(配置:配置)

let sessionManager = Alamofire.SessionManager(configuration: configuration)

我更改了配置以对应后台配置

I changed my configuration to correspond background configuration

现在看起来像这样

let configuration = URLSessionConfiguration.background(withIdentifier: "com.room.myApp") configuration.timeoutIntervalForRequest = 10 // seconds alamoFireManager = Alamofire.SessionManager(configuration: configuration) alamoFireManager.upload(deligate.data!, to: deligate.url, method: .post, headers: headers) .uploadProgress(closure: { progress in print("Upload Progress: \(progress.fractionCompleted)") }) .validate() .responseJSON {}

我得到错误

*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Upload tasks from NSData are not supported in background sessions.' *** First throw call stack: (0x18ec511b8 0x18d68855c 0x18f33808c 0x18f33796c 0x18f336e68 0x100ef9218 0x100f05dc8 0x18f336dc8 0x18f3378cc 0x100255890 0x1002518e8 0x100234200 0x100234448 0x100ef9218 0x100f05dc8 0x100233fc4 0x100255290 0x10029d238 0x10029ae4c 0x10029ac34 0x10006dd78 0x100071044 0x100082708 0x10002b310 0x100ef9258 0x100ef9218 0x100f0726c 0x100f08e2c 0x100f08b78 0x18dce32a0 0x18dce2d8c) libc++abi.dylib: terminating with uncaught exception of type NSException

我在做什么错?

是从我这边还是从lib方面发出的?

Is it issue from my side or lib side?

随意问

编辑

有发送流

这是我创建zip文件的方式

There is how I create zip file

internal func madeRequest() { DispatchQueue.global().async { if self.createZipPhotoDir() { self.appDeligate.log.debug("zip dir was created") self.serverConnection.makeServerRequest() } else { self.appDeligate.log.error("can NOT execute createZipPhotoDir()") } } } private func createZipPhotoDir() -> Bool { let withContentsOfDirectory: String! = UtilDirectory.pathToMyCurrentAvatarDir.tweak() // "/var/mobile/Containers/Data/Application/739A895E-7BCA-47A8-911F-70FBC812CEB3/Documents/default@domein/AvatarPackage/name/" let atPath: String! = UtilDirectory.tempZipPath.tweak() // "/var/mobile/Containers/Data/Application/739A895E-7BCA-47A8-911F-70FBC812CEB3/Documents/default@domein/tmpZipDir.zip" return SSZipArchive.createZipFile(atPath: atPath, withContentsOfDirectory: withContentsOfDirectory) }

zip文件创建成功

然后我发出服务器请求

required init() { configureAlamofireManager() } private func configureAlamofireManager() { let configuration = URLSessionConfiguration.background(withIdentifier: "com.fittingroom.newtimezone.Fitzz") alamoFireManager = Alamofire.SessionManager(configuration: configuration) } internal func makeServerRequest() { appDeligate.log.debug("request was sended") alamoFireManager.upload(deligate.data!, to: deligate.url, method: .post, headers: headers) .uploadProgress(closure: { progress in print("Upload Progress: \(progress.fractionCompleted)") }) .validate() .responseJSON { [weak self] response in self?.appDeligate.log.debug("response : \(response)") self?.appDeligate.log.debug(String(describing: response.timeline)) switch response.result { case .success(let value): self?.appDeligate.log.debug("succes in server connection response") let result = self?.getStatusCodeAndData(json: JSON(value)) self?.deligate.onSuccess(statusCode: result?.statusCode, data: result?.data) case .failure(let error): self?.appDeligate.log.error("error in UploadingRequest : \(error)") self?.deligate.onError() } } }

有一种获取数据的方式

internal var data: Data { var data = Data() let filePath = UtilDirectory.tempZipPath.tweak() if let result = UtilFile.exists(path: filePath), result.isFileExist == true, result.whatIsIt == .file { if let fileData = FileManager.default.contents(atPath: filePath) { data = fileData appDeligate.log.debug("*** DATA : \(data) ***") } else { print("Could not parse the file") } } else { appDeligate.log.error("some ERROR here: file not exist") } return data }

推荐答案

来自背景传输注意事项 :

仅上传任务支持文件(在程序退出后从数据对象上载或流将失败)。

Only upload tasks from a file are supported (uploading from data objects or a stream will fail after the program exits).

这意味着它是NSURLSession的限制-您需要从文件上传,然后尝试解决文件中的其他错误

that means it is limitation from NSURLSession - you need you upload from a file and then try to solve the other error with file

更新

appDeligate.log.debug("request was sended") let tempZipFilePath = UtilDirectory.tempZipPath.tweak() alamoFireManager.upload(tempZipFilePath, to: deligate.url, method: .post, headers: headers)

更多推荐

如何执行alamofire后台上传请求?

本文发布于:2023-11-26 03:05:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632383.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:后台   上传   alamofire

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!