Swift:当用户关闭应用程序时将图像上传到Firebase云存储 [英] Swift: Uploading image to Firebase cloud storage when user closes app

查看:100
本文介绍了Swift:当用户关闭应用程序时将图像上传到Firebase云存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户终止应用程序时,如何将图像上传到Firebase存储?

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

这是我的代码:

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
   }

如您所见,如果用户终止应用程序并且我无法将用户的图像上传到Firebase Storage,则异步完成块将永远不会完成

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

如何防止用户上传图片后终止应用程序?我有一个初步的想法.我可以暂时将图像存储在CoreData/NSUserDefaults中,并在用户再次打开应用程序时重试上传吗?

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?

推荐答案

好问题.目前,在iOS上,如果应用程序被后台或终止,则无法持久并重新启动上载或下载.此功能在Android上存在(因为在屏幕上重置了活动"旋转,这是一个更为常见的问题),我们计划将来在iOS上提供该功能.

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.

最终,我们只想将此标记设置为[FIRStorage enablePersistentUploads:YES]或类似标记即可自动为您执行此操作,因此您完全不必考虑它.

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.

这篇关于Swift:当用户关闭应用程序时将图像上传到Firebase云存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆