Firebase Storage claims here in its iOS documentation that it
performs uploads and downloads regardless of network quality. Uploads and downloads are robust, meaning they restart where they stopped
so one would expect it to handle a loss of connection when uploading, but it doesn't seem to.
With the following Swift code in iOS, I am able to perform an upload just fine when there is a connection, but if the device doesn't have a connection or if it is ever disconnected from the network it goes to the failure condition.
let storage = FIRStorage.storage().referenceForURL("VALID_URL_REMOVED")
let imagesRef = storage.child("images/test.jpg")
let data = UIImageJPEGRepresentation(observationImage!, 0.7);
let uploadTask = imagesRef.putData(data!, metadata: nil)
uploadTask.observeStatus(.Progress) { snapshot in
// Upload reported progress
if let progress = snapshot.progress {
let percentComplete = 100.0 * Double(progress.completedUnitCount) / Double(progress.totalUnitCount)
print("percent \(percentComplete)")
}
}
uploadTask.observeStatus(.Success) { snapshot in
// Upload completed successfully
print("success")
}
uploadTask.observeStatus(.Failure) { snapshot in
print("error")
print(snapshot.error?.localizedDescription)
}
The debug output for this code is as follows.
/*
percent 0.0
percent 0.0044084949781492
2016-06-30 11:49:16.480 Removed[5020:] <FIRAnalytics/DEBUG> Network status has changed. Code, status: 1, Disconnected
percent 0.0044084949781492
error
Optional("An unknown error occurred, please check the server response.")
*/
Firebase's Real Time Database offline storage is also set up with the following code, but I'm unsure of whether this is related.
FIRDatabase.database().persistenceEnabled = true
I have also tried manually setting the timeout as mentioned in the answers to this question using the following lines, with no change.
let config = FIRStorage()
config.maxUploadRetryTime = 1000000
Is there a way to have it handle these disconnects without implementing the functionality from scratch? Am I missing something?
You are missing observers. Right now you only observe .success and .failure events. Try add observers for .resume, .pause, .progress to handle different events.
// Listen for state changes, errors, and completion of the upload.
uploadTask.observe(.resume) { snapshot in
// Upload resumed, also fires when the upload starts
}
uploadTask.observe(.pause) { snapshot in
// Upload paused
}
uploadTask.observe(.progress) { snapshot in
// Upload reported progress
let percentComplete = 100.0 * Double(snapshot.progress!.completedUnitCount)
/ Double(snapshot.progress!.totalUnitCount)
}
uploadTask.observe(.failure) { snapshot in
if let error = snapshot.error as? NSError {
switch (FIRStorageErrorCode(rawValue: error.code)!) {
case .objectNotFound:
// File doesn't exist
break
case .unauthorized:
// User doesn't have permission to access file
break
case .cancelled:
// User canceled the upload
break
/* ... */
case .unknown:
// Unknown error occurred, inspect the server response
break
default:
// A separate error occurred. This is a good place to retry the upload.
break
}
}
}
Related
I'm having trouble using network framework to transfer a collection of 1 minute video files between a recording device and a repository master device. All devices are on the same wifi. But the problem is that the distance between devices somehow is player a role in the transfer. When I have strong wifi signal on both devices, but if the devices are further than 100 feet apart, the transfer won't happen. Conversely, if both devices have very weak wifi signal (e.g. 1 bar), but are next to each other, the transfer happens without a hitch. It's as if the devices are doing something more akin to wifi direct than transfer for a LAN, which is what I really want. I'm assuming someone familiar with the framework can advise how to entirely avoid using any peer to peer wifi direct type of transfer, and force the system to transfer over the WLAN. That way all I need to do is make sure all devices have a decent wifi signal (e.g. are on the network with a decent connection), and regardless of how far they are physically from each other, the files will transfer without a hitch. Hope someone can clear this up for me.
Thanks.
Sending files between 2 devices at close range
disabled Bluetooth
Made sure both devices are on the same local network.
#Paulw11 - Thank you for the response, I have provided more details that I should have provided initially, I do apologize for that. Thanks again for the help.
We are using the mDNS approach.
func startBrowsing() {
guard browser == nil else { return }
let params = NWParameters()
params.includePeerToPeer = true
params.requiredInterfaceType = .wifi
let browser = NWBrowser(for: .bonjour(type: bonjourService, domain: nil), using: params)
self.browser = browser
browser.browseResultsChangedHandler = {
[weak self] results, changes in
guard let self = self else { return }
self.delegate?.peerBrowser(self, didUpdateResults: Array(results))
}
browser.stateUpdateHandler = { [weak self] newState in
guard let self = self else { return }
os_log("[browser] %#", newState.debugDescription)
switch newState {
case .cancelled:
break
case .failed:
os_log("[browser] restarting")
self.browser?.cancel()
self.startBrowsing()
case .ready:
break
case .setup:
break
#unknown default:
break
}
}
browser.start(queue: .main)
}
There are no such errors. I can see the standard communication is still working and because of that the sender(iPhone) will continue to send the files to iPad (receiver) with out fail but the receiver couldn’t able to receive it.
3.No
The code used to send data from one peer to another.
func sendMessage(type: UInt32, content: Data) {
guard connection?.state == .ready else {
return
}
let framerMessage = NWProtocolFramer.Message(messageType: type)
let context = NWConnection.ContentContext(identifier: "Message", metadata: [framerMessage])
connection?.send(content: content, contentContext: context, isComplete: true, completion: .idempotent)
}
My app (Xcode 9.2, Swift 4) uses UIManagedDocument as a basic Core Data stack. Everything was working fine for months but lately I've noticed several cases where the app won't load for existing users because the core data init isn't completing. This usually happens after a crash in the app (I think but not sure).
I've been able to recreate the problem on the debugger and narrowed the problem down to the following scenario:
App starts up --> core data is called to start up --> UIManagedDocument object is init'd --> check doc status == closed --> call open() on doc --> open never completes - the callback closure is never called.
I've subclassed UIManagedDocument so I could override configurePersistentStoreCoordinator() to check if it ever reaches that point but it doesn't. The subclass override for handleError() is never called either.
The open() process never reaches that point. What I can see if I pause the debugger is that a couple of threads are blocked on mutex/semaphore related to the open procedure:
The 2nd thread (11) seems to be handling some kind of file conflict but I can't understand what and why. When I check documentState just before opening the file I can see its value is [.normal, .closed]
This is the code to init the doc - pretty straight forward and works as expected for most uses and use cases:
class MyDataManager {
static var sharedInstance = MyDataManager()
var managedDoc : UIManagedDocument!
var docUrl : URL!
var managedObjContext : NSManagedObjectContext {
return managedDoc.managedObjectContext
}
func configureCoreData(forUser: String, completion: #escaping (Bool)->Void) {
let dir = UserProfile.profile.getDocumentsDirectory()
docUrl = dir.appendingPathComponent(forUser + GlobalDataDocUrl, isDirectory: true)
managedDoc = UIManagedDocument(fileURL: docUrl)
//allow the UIManagedDoc to perform lieghtweight migration of the DB in case of small changes in the model
managedDoc.persistentStoreOptions = [
NSMigratePersistentStoresAutomaticallyOption: true,
NSInferMappingModelAutomaticallyOption: true
]
switch (self.managedDoc.documentState)
{
case UIDocumentState.normal:
DDLogInfo("ManagedDocument is ready \(self.docUrl)")
case UIDocumentState.closed:
DDLogInfo("ManagedDocument is closed - will open it")
if FileManager.default.fileExists(atPath: self.docUrl.path) {
self.managedDoc.open() { [unowned self] (success) in
DDLogInfo("ManagedDocument is open result=\(success)")
completion(success)
}
}
else{
self.managedDoc.save(to: self.managedDoc.fileURL, for: .forCreating) { [unowned self] (success) in
DDLogInfo("ManagedDocument created result=\(success) ")
completion(success)
}
}
case UIDocumentState.editingDisabled:
fallthrough
case UIDocumentState.inConflict:
fallthrough
case UIDocumentState.progressAvailable:
fallthrough
case UIDocumentState.savingError:
fallthrough
default:
DDLogWarn("ManagedDocument status is \(self.managedDoc.documentState.rawValue)")
}
}
}
Again - the closure callback for managedDoc.open() never gets called. It seems like the file was left in some kind of bad state and cannot be opened.
BTW, if I copy the app container from the device to my mac and open the SQLLite store I can see everything is there as expected.
Is there a way or delegate to catch the updated values while the app is running without terminating the app. I am using this method to fetch the values and update.
RemoteConfig.remoteConfig().fetch(withExpirationDuration: duration) { [weak self] (status, error) in
guard error == nil else {
print("Got an error fetching remote values \(error!)")
return
}
print ("Retrieved values from the cloud!")
RemoteConfig.remoteConfig().activateFetched()
guard let strongSelf = self else { return }
strongSelf.updateWithRomteConfigValues()
}
The activateFetched() call will make sure to get the latest update data (either from the defaults or the remote config) without needing to terminate the app.
I think the problem in your case come from the duration.
Try setting the duration to 0 (make sure to only do it if the developer mode is enabled )
I have an application that need to track user heart rate readings from apple watch, so I did all the required steps that I found on apple guides, and here is the code that I am using:
static var query: HKObserverQuery?
func startObservingHeartRate() {
guard let heartRateSampleType = HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate) else {
fatalError("Unable to create a step count sample type")
}
AppDelegate.query = HKObserverQuery(sampleType: heartRateSampleType, predicate: nil, updateHandler: { (query, completionHandler, error) in
if error != nil {
// Perform Proper Error Handling Here...
print("An error occured while setting up the Heart Rate observer.")
}
//Read the last strored heatt rate in add it to the DB
//Add last fetched Heart Rate reading to DB and send it to clips
HealthKitManager().fetchLastStoredHeartRate(completion: { (lastReading, error) in
guard let lastReading = lastReading else {
//There is no heart readings in HealthKit
return
}
//Check if Last HR value is Abnormal
if lastReading.doubleValue > 60 {
//TODO: - Schedule notification
if UIApplication.shared.applicationState == .background {
} else {
//TODO: - Show popup to the user
}
}
})
completionHandler()
})
healthKitStore.execute(AppDelegate.query!)
configureHeartRateObserver()
}
func configureHeartRateObserver() {
guard let heartRateSampleType = HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate) else {
fatalError("Unable to create a step count sample type")
}
healthKitStore.enableBackgroundDelivery(for: heartRateSampleType, frequency: HKUpdateFrequency.immediate) { (success, error) in
if success {
print("Enabled background delivery of Heart Rate changes")
} else {
print("Failed to enable background delivery of weight changes. ")
}
}
}
and I am calling "startObservingHeartRate" in didFinishLaunchingWithOptions in AppDelegate, assuming that this query should be executed once a new reading added or deleted from the health kit store, every thing is fine, if app is in background or killed the handler wake up my app and it do the updates.
But whenever I put the app in background then put it in foreground again it execute the observer query for many times even if there is no new readings added to the HealthKit store and in this case I am getting the same last heart rate for many times for no reason.
Please any recommendation on how to use this types of query or any changes I need to do with my current implementation.
If you want to track added and removed heart rate samples more precisely, you should use an HKAnchoredObjectQuery. HKObserverQuery does not guarantee that its update handler will only be called when a sample is added or removed. Note that you must continue executing an HKObserverQuery in addition to HKAnchoredObjectQuery since you are also using enableBackgroundDelivery(for:frequency:completion:).
I am downloading an image from Firebase storage as follows:
let storage = FIRStorage.storage()
// Create a storage reference from our storage service
let storageRef = storage.reference(forURL: "MY_STORAGE_URL")
let imageRef = storageRef.child("Path_to_image")
// Download image in memory
let downloadTask = imageRef.data(withMaxSize: 1 * 1024 * 1024) {
(data, error) -> Void in
if (error != nil) {
//Handle the error
} else {
guard let imageData = data else {
print("Unable to unwrap image data.")
return
}
let downloadedImage = UIImage(data: imageData)
//Do some stuff with the image
}
}
I am also monitoring what happens with the download using the following observers:
// Observe changes in status
downloadTask.observe(.resume) { (snapshot) -> Void in
// Download resumed, also fires when the download starts
}
downloadTask.observe(.pause) { (snapshot) -> Void in
// Download paused
}
downloadTask.observe(.progress) { (snapshot) -> Void in
// Download reported progress
}
downloadTask.observe(.success) { (snapshot) -> Void in
// Download completed successfully
}
downloadTask.observe(.failure) { (snapshot) -> Void in
//Download failed
}
This all works just fine when the app is first started. However, I am getting problems if the app enters the background and I play around with some other applications (Facebook, Twitter, etc.), then bring the app back to the foreground. I also have problems if I leave the app open and running in the foreground for greater than or equal to 1 hour.
The problem is that the completion handler in let downloadTask = imageRef.data(withMaxSize: blah blah blah (in the first block of code above) is never called. If the completion handler is never called, I can never unwrap the data and attempt to use the image in my application.
Also, in the downloadTask observers, the only completion handlers that get fired are .resume and .progress. The .success or .failure events are never triggered. This seems to be a Firebase Storage bug to me, but I am not sure. Has anyone else encountered a similar issue? I don't understand why the code would work just fine from a fresh launch, but then after some time in the foreground or after some time in the background the image download stops working. Thanks in advance for any input you may have.
This is currently the expected behavior, unfortunately. Firebase Storage (at present) is foreground only: if the app is backgrounded, we haven't persisted the upload URL, and can't upload in the background nor restart it after it gets out of the background, so it probably is killed by the OS and the item isn't uploaded.
It's The Next Big Thing™ we'd like to tackle (our Android SDK makes it possible, though not easy), but unfortunately for now we haven't made more progress on this.
As a bit of a side note, your observers won't exist after the activity change--downloadTask is gone once the app is backgrounded, so when it comes back into the foreground, we basically need a method that retrieves all tasks that are currently backgrounded, and allows you to hook observers back up. Something like:
FIRStorage.storage().backgroundedTasks { (tasks) -> Void in
// tasks is an array of upload and download tasks
// not sure if it needs to be async
}