leonardloo
leonardloo

Reputation: 1803

Swift: Uploading image to Firebase cloud storage when user closes app

How do I upload an image to Firebase Storage when the user terminates the app?

Here's my code:

firebaseRef.child("\(uid)/\(filePath)").put(data, metadata: metadata) { (metadata, error) in
   // NOTE: This spot never reaches when user closes the app
   if error != nil || metadata == nil {
      log.error("Fail to store image in cloud storage")
   } else {
      // Do something with the metadata url
   }

As you can see, the async completion block never completes if the user terminates the app and I'm not able to upload the user's image to Firebase Storage

How do I safeguard against user terminating the app after uploading the picture? I have a preliminary idea. Could I store the image in CoreData/NSUserDefaults temporarily, and retry the upload when the user opens the app again?

Upvotes: 3

Views: 1361

Answers (1)

Mike McDonald
Mike McDonald

Reputation: 15963

Good question. At present, on iOS, there's no way of persisting and restarting the upload or download if the app is backgrounded or killed. This feature exists on Android (since the Activity is reset on screen rotation, making this a far more common issue), and we're planning on making it available on iOS in the future.

Eventually, we want to simply make this a flag [FIRStorage enablePersistentUploads:YES] or similar, to automatically do this for you, so you don't have to think about it at all.

Upvotes: 2

Related Questions