@@ -115,38 +115,36 @@ import Foundation
115
115
116
116
// Process fetches
117
117
self . state = . running
118
+ Task {
119
+ do {
120
+ let data = try await self . uploadFetcher? . beginFetch ( )
121
+ // Fire last progress updates
122
+ self . fire ( for: . progress, snapshot: self . snapshot)
123
+
124
+ // Upload completed successfully, fire completion callbacks
125
+ self . state = . success
118
126
119
- self . fetcherCompletion = { [ self ] ( data: Data ? , error : NSError ? ) in
120
- // Fire last progress updates
121
- self . fire ( for : . progress , snapshot : self . snapshot )
127
+ guard let data = data else {
128
+ fatalError ( " Internal Error: uploadFetcher returned with nil data and no error " )
129
+ }
122
130
123
- // Handle potential issues with upload
124
- if let error {
131
+ if let responseDictionary = try ? JSONSerialization
132
+ . jsonObject ( with: data) as? [ String : AnyHashable ] {
133
+ let metadata = StorageMetadata ( dictionary: responseDictionary)
134
+ metadata. fileType = . file
135
+ self . metadata = metadata
136
+ } else {
137
+ self . error = StorageErrorCode . error ( withInvalidRequest: data)
138
+ }
139
+ self . finishTaskWithStatus ( status: . success, snapshot: self . snapshot)
140
+ } catch {
141
+ self . fire ( for: . progress, snapshot: self . snapshot)
125
142
self . state = . failed
126
- self . error = StorageErrorCode . error ( withServerError: error, ref: self . reference)
143
+ self . error = StorageErrorCode . error ( withServerError: error as NSError ,
144
+ ref: self . reference)
127
145
self . metadata = self . uploadMetadata
128
146
self . finishTaskWithStatus ( status: . failure, snapshot: self . snapshot)
129
- return
130
- }
131
- // Upload completed successfully, fire completion callbacks
132
- self . state = . success
133
-
134
- guard let data = data else {
135
- fatalError ( " Internal Error: fetcherCompletion returned with nil data and nil error " )
136
- }
137
-
138
- if let responseDictionary = try ? JSONSerialization
139
- . jsonObject ( with: data) as? [ String : AnyHashable ] {
140
- let metadata = StorageMetadata ( dictionary: responseDictionary)
141
- metadata. fileType = . file
142
- self . metadata = metadata
143
- } else {
144
- self . error = StorageErrorCode . error ( withInvalidRequest: data)
145
147
}
146
- self . finishTaskWithStatus ( status: . success, snapshot: self . snapshot)
147
- }
148
- self . uploadFetcher? . beginFetch { [ weak self] ( data: Data ? , error: Error ? ) in
149
- self ? . fetcherCompletion ? ( data, error as NSError ? )
150
148
}
151
149
}
152
150
}
@@ -202,7 +200,6 @@ import Foundation
202
200
}
203
201
204
202
private var uploadFetcher : GTMSessionUploadFetcher ?
205
- private var fetcherCompletion : ( ( Data ? , NSError ? ) -> Void ) ?
206
203
private var uploadMetadata : StorageMetadata
207
204
private var uploadData : Data ?
208
205
// Hold completion in object to force it to be retained until completion block is called.
@@ -247,7 +244,6 @@ import Foundation
247
244
func finishTaskWithStatus( status: StorageTaskStatus , snapshot: StorageTaskSnapshot ) {
248
245
fire ( for: status, snapshot: snapshot)
249
246
removeAllObservers ( )
250
- fetcherCompletion = nil
251
247
}
252
248
253
249
private func GCSEscapedString( _ input: String ? ) -> String ? {
0 commit comments