这就是我的completionHandler的样子:
self.completionHandler = { (task, error) -> Void in
DispatchQueue.main.async(execute: {
if ((error) != nil){
//retry upload
}
else{
//success
}
})
}
我看不到任何方法可以调用task
重试,我只看到continue
方法,但不知道如何使用它。
我第一次开始上传的方式如下:
transferUtility.uploadData(
data as Data,
bucket: "test",
key: "testTU3/try(Date()).jpeg",
contentType: "image/jpeg",
expression: expression,
completionHander: completionHandler).continue(successBlock: { (task) -> AnyObject! in
if let error = task.error {
NSLog("Error: %@",error.localizedDescription);
}
if let exception = task.exception {
NSLog("Exception: %@",exception.description);
}
if let _ = task.result {
NSLog("Upload Starting!")
// Do something with uploadTask.
}
return nil;
})
失败后如何在completionHandler中重试上载?
编辑:
这是我试过的
self.completionHandler = { (task, error) -> Void in
DispatchQueue.main.async(execute: {
if ((error) != nil){
guard let data = task.request?.httpBody else {return}
self.uploadData(data: data as NSData, bucket: task.bucket, key: task.key)
}
else{
}
})
}
但问题是,自从以来,我无法获得请求的实际数据
task.request?.httpBody
评估为零。
这样做是个好主意吗?我知道这可能会导致无休止的循环
Transfer Utility不支持重试,失败后您必须自己进行调用。
关于您的代码。如果调用continue(successBlock:)
方法,则只有在上传成功的情况下才会执行块内的代码。还有另一种变体continue(block:)
,即使出现错误也会执行。