在异步函数上同时获取进度和错误操作
Get Progress and Error operations with the same time on Async functions
我在主页 VC 中使用 Alamofire 上传了一个文件。但是在class函数中上传func。所以我没有得到进度操作。我想我必须写委托方法。
有人会帮助我吗?
我的问题在ViewController!!!
在SendPhoto.class
class SendPhoto {
func send() {
Alamofire.upload(
multipartFormData: { multipartFormData in
...
},
to: "",
headers:headers,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.uploadProgress {progress in
print(progress.fractionCompleted)
}
upload.responseJSON { response in
}
case .failure(let encodingError):
print("File Upload Error")
print(encodingError)
}
})
}}
在ViewController
class HomepageVC: UIViewController .. {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func sendAction(_ sender: Any) {
SendPhoto().send()
// I want to keep this function progress and print to a label or etc. !!!
}
}
但是你的问题没有完成所以假设
Alamofire returns 任何上传请求的 UploadRequest 对象,除了 multipart 请求
上传请求有属性进度
/// The progress of fetching the response data from the server for the request.
open var progress: Progress { return dataDelegate.progress }
可用于跟踪上传进度
编辑
您必须通过闭包将数据传递给您的视图控制器
这是你的方法
private func uploadAnyThing (progress:@escaping ((Progress) -> Void) , completed:@escaping ((_ success:Bool,_ error:Error?) -> Void)) {
Alamofire.upload(multipartFormData: { (data) in
}, to: "test.com") { (result) in
switch (result) {
case .success(let request, _, _):
request.uploadProgress(closure: { (prog) in
progress(prog)
})
request.responseJSON(completionHandler: { (res) in
completed(true,nil)
})
case .failure(let error) :
completed(false,error)
break
}
}
}
你可以通过
获得
func test () {
uploadAnyThing(progress: { (progres) in
print(progres.fractionCompleted)
}) { (success, error) in
}
}
我在主页 VC 中使用 Alamofire 上传了一个文件。但是在class函数中上传func。所以我没有得到进度操作。我想我必须写委托方法。
有人会帮助我吗?
我的问题在ViewController!!!
在SendPhoto.class
class SendPhoto {
func send() {
Alamofire.upload(
multipartFormData: { multipartFormData in
...
},
to: "",
headers:headers,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.uploadProgress {progress in
print(progress.fractionCompleted)
}
upload.responseJSON { response in
}
case .failure(let encodingError):
print("File Upload Error")
print(encodingError)
}
})
}}
在ViewController
class HomepageVC: UIViewController .. {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func sendAction(_ sender: Any) {
SendPhoto().send()
// I want to keep this function progress and print to a label or etc. !!!
}
}
但是你的问题没有完成所以假设
Alamofire returns 任何上传请求的 UploadRequest 对象,除了 multipart 请求
上传请求有属性进度
/// The progress of fetching the response data from the server for the request.
open var progress: Progress { return dataDelegate.progress }
可用于跟踪上传进度
编辑
您必须通过闭包将数据传递给您的视图控制器
这是你的方法
private func uploadAnyThing (progress:@escaping ((Progress) -> Void) , completed:@escaping ((_ success:Bool,_ error:Error?) -> Void)) {
Alamofire.upload(multipartFormData: { (data) in
}, to: "test.com") { (result) in
switch (result) {
case .success(let request, _, _):
request.uploadProgress(closure: { (prog) in
progress(prog)
})
request.responseJSON(completionHandler: { (res) in
completed(true,nil)
})
case .failure(let error) :
completed(false,error)
break
}
}
}
你可以通过
获得func test () {
uploadAnyThing(progress: { (progres) in
print(progres.fractionCompleted)
}) { (success, error) in
}
}