在不利的networking条件下,Firebase存储上传失败

Firebase Storage在其iOS文档中声称它

不pipenetworking质量如何,执行上传和下载。 上传和下载function强大,这意味着他们在停止的地方重新开始

所以人们会期望它在上传时处理丢失的连接,但似乎并没有。

在iOS中使用下面的Swift代码时,我可以在有连接的情况下执行上传,但是如果设备没有连接或者从networking断开连接,就会进入失败状态。

let storage = FIRStorage.storage().referenceForURL("VALID_URL_REMOVED") let imagesRef = storage.child("images/test.jpg") let data = UIImageJPEGRepresentation(observationImage!, 0.7); let uploadTask = imagesRef.putData(data!, metadata: nil) uploadTask.observeStatus(.Progress) { snapshot in // Upload reported progress if let progress = snapshot.progress { let percentComplete = 100.0 * Double(progress.completedUnitCount) / Double(progress.totalUnitCount) print("percent \(percentComplete)") } } uploadTask.observeStatus(.Success) { snapshot in // Upload completed successfully print("success") } uploadTask.observeStatus(.Failure) { snapshot in print("error") print(snapshot.error?.localizedDescription) } 

此代码的debugging输出如下所示。

 /* percent 0.0 percent 0.0044084949781492 2016-06-30 11:49:16.480 Removed[5020:] <FIRAnalytics/DEBUG> Network status has changed. Code, status: 1, Disconnected percent 0.0044084949781492 error Optional("An unknown error occurred, please check the server response.") */ 

Firebase的实时数据库离线存储也使用以下代码进行设置,但我不确定这是否与此相关。

 FIRDatabase.database().persistenceEnabled = true 

我也尝试使用以下几行手动设置该问题的答案中提到的超时值,但没有任何更改。

 let config = FIRStorage() config.maxUploadRetryTime = 1000000 

有没有办法让它处理这些断开而不从头开始实现function? 我错过了什么吗?

你错过了观察者。 现在你只能观察成功和失败的事件。 尝试为.resume,.pause,.progress添加观察者来处理不同的事件。

 // Listen for state changes, errors, and completion of the upload. uploadTask.observe(.resume) { snapshot in // Upload resumed, also fires when the upload starts } uploadTask.observe(.pause) { snapshot in // Upload paused } uploadTask.observe(.progress) { snapshot in // Upload reported progress let percentComplete = 100.0 * Double(snapshot.progress!.completedUnitCount) / Double(snapshot.progress!.totalUnitCount) } uploadTask.observe(.failure) { snapshot in if let error = snapshot.error as? NSError { switch (FIRStorageErrorCode(rawValue: error.code)!) { case .objectNotFound: // File doesn't exist break case .unauthorized: // User doesn't have permission to access file break case .cancelled: // User canceled the upload break /* ... */ case .unknown: // Unknown error occurred, inspect the server response break default: // A separate error occurred. This is a good place to retry the upload. break } } }