
Firebase is a great technology which does most of your backend tasks without needing your own server. You also don’t need to write APIs to store and get the data. Firebase provides all the necessary APIs.
It also unifies your efforts if you are developing iOS as well as Android applications in the sense you could use same API’s, Push Notification interface, App Analytics, etc through a single interface.
Serving JSON feeds through firebase helps to save performance of your own server especially if you are expecting huge downloads.
It can be achieved very easily using below mentioned steps
Below code is written in Swift 3.
It is assumed that you have set up Public Read permissions on your Firebase Storage Account.
Below is a screenshot showing how to set Public Read Permissions

Import FireBase
1 |
import FirebaseStorage |
Create a reference to your Firebase storage
1 |
let storageRef = Storage.storage().reference() |
Setting the path for the local file (which will be eventually stored on the local device)
1 2 3 4 |
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) let documentsDirectory = paths[0] let filePath = "file:\(documentsDirectory)/filename.txt" let fileURL = URL.init(string: filePath) |
Download the firebase file and store it on the local device
1 2 3 4 5 6 7 8 9 10 11 |
// [START downloadimage] storageRef.child("path/to/firebase/file/filename.txt").write(toFile: fileURL!, completion: { (url, error) in if let error = error { print("Error downloading:\(error)") return } else if (url?.path) != nil { } }) // [END downloadimage] |
Here we download the file and store it locally so next time the feed gets parsed from the local device rather than downloading it again
Now finally, how to update the local file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
Storage.storage().reference().child("path/to/firebase/file/filename.txt").getMetadata { metadata, error in if error != nil { } else { //Get Local file created date let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) let filePath = "\(paths[0])/filename.txt" if FileManager.default.fileExists(atPath: filePath) { do{ let attrs = try FileManager.default.attributesOfItem(atPath: filePath) as NSDictionary if((metadata?.updated!)! > attrs.fileCreationDate()!){ //Download the file from firebase (As shown in the above snippet) } else { print("File not required to be downloaded") } } catch let error as NSError { print(error) } } else { //Download the file from firebase (As shown in the above snippet) } } } |
Here we get the metadata of the firebase file to check the updated date. We can compare the date with the creation date of the corresponding local file and then decide whether or not to download the firebase file again.