I’m importing small recordsdata to Firebase Storage, roughly 276KB a bit. The app can select to do a small handful of those uploads per day from the background. The specified habits is that each time the app decides to add the uploads can begin in background, and end whereas the app is in background with out the consumer needing to work together.
My present code is that this. This logic works in case your machine is in good circumstances. But when your machine is low energy, or working on mobile knowledge than the system appears to dam the add.
public func uploadAudioToFirebase(fileName: String, audioPath: URL, completion: @escaping (End result) -> Void) {
let storage = Storage.storage()
let storageRef = storage.reference()
let audioName = fileName + ".m4a"
let audioRef = storageRef.little one("audios/" + audioName)
let metadata = StorageMetadata()
metadata.contentType = "audio/x-m4a"
audioRef.putFile(from: audioPath, metadata: metadata) { metadata, error in
if let error = error {
completion(.failure(error))
return
}
guard let uploadMetadata = metadata else {
completion(.failure(NSError(area: "MetadataError", code: -1, userInfo: [NSLocalizedDescriptionKey: "No metadata available"])))
return
}
audioRef.downloadURL { (url, error) in
guard let downloadURL = url else {
completion(.failure(error!))
return
}
completion(.success(downloadURL.absoluteString))
}
}
}
First I attempted to create even smaller recordsdata to add. I examined with file round 145KB in measurement, however this didn’t appear to alter the habits.
I believed to make use of the Firebase REST API and URLSessions
or NSURLSession
with discretionary
set to false, to take management of when the system chooses to add. However in response to the docs, if the add begins from the background the system will mechanically set discretionary
to true. Giving management again to the system to decide on when the add occurs.
I’ve tried wrapping my Storage add logic in a beginBackgroundTask
. However so far as I can inform that is having the identical habits as if it was not there.
public func uploadAudioToFirebase(fileName: String, audioPath: URL, completion: @escaping (End result) -> Void) {
var taskId: UIBackgroundTaskIdentifier = .invalid
taskId = UIApplication.shared.beginBackgroundTask(withName: "AudioUpload") {
UIApplication.shared.endBackgroundTask(taskId)
taskId = .invalid
}
let storage = Storage.storage()
let storageRef = storage.reference()
let audioName = fileName + ".m4a"
let audioRef = storageRef.little one("audios/(audioName)")
let metadata = StorageMetadata()
metadata.contentType = "audio/x-m4a"
audioRef.putFile(from: audioPath, metadata: metadata) { metadata, error in
if let error = error {
completion(.failure(error))
if taskId != .invalid {
UIApplication.shared.endBackgroundTask(taskId)
}
return
}
audioRef.downloadURL { (url, error) in
if let error = error {
completion(.failure(error))
} else if let downloadURL = url {
completion(.success(downloadURL.absoluteString))
}
if taskId != .invalid {
UIApplication.shared.endBackgroundTask(taskId)
}
}
}
}
My subsequent concept was to make use of the BGTaskScheduler
with BGAppRefreshTask
. However I do not know if it will really change the present habits. So far as I can inform BGAppRefreshTask
will let the system select when to periodically do small duties. Which I feel would be the identical time that it’s already selecting to, ie when it has WiFi and good battery life.
It makes it more durable that I do not know the way the Firebase SDK putFile
works below the hood. It appears to do some form of background administration already, it might simply utilizing the BGTaskScheduler
already.
I’m at a roadblock now, I’m not positive find out how to proceed. I’ve learn that you just would possibly be capable to use Silent Push Notifications for one thing like this? This has limitation although, and I’m not sure how nicely it really works with a Firebase add.
Is there a extra straight ahead approach to power a small add that I’m lacking?