Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase FIRStorageUploadTask On Swift while app is in Background state

I'd like to implement a file upload queue for my app. Files will be uploaded to Firebase Storage. I am able to upload the files using background thread while app is in foreground mode, my challenge is to continue the upload while the app is in background state. From my research so far, using NSURLSession an app can fetch data from the network but I am not sure how to use this with Firebase API.

Thanks in advance.

like image 359
Richard Avatar asked Sep 01 '16 14:09

Richard


1 Answers

Question was asked ~6months ago but I was trying to solve the same problem so stumbled upon it. If it helps, I was able to solve this by setting up the standard iOS background task:

backgroundUploadID = UIApplication.shared.beginBackgroundTask(expirationHandler: nil)

Did this before the call to:

uploadTask = firebaseStorageRef.put(data!, metadata: metadata)

All the upload observers - .progress, .success & .failure - are called when the app is in the background. In case of '.success', 'failure' call:

UIApplication.shared.endBackgroundTask(currentBackgroundUploadID)

I assume that if my uploads were huge, the OS would have cut me off at some point. I'm uploading iPhone camera photos ~3mb each.

like image 103
Shai Ben-Tovim Avatar answered Nov 06 '22 19:11

Shai Ben-Tovim