FirebaseStorageCacheable
Overview
FirebaseStorageCacheable is a libary to that downloads and caches the latest version of a file Firebase Storage.
This library allows you download and cache files hosted in Firebase Storage.
It uses the timestamps of your local file, compared to the timestamp of the remote file (by using the file meta information), to determine if the remote file is newer.
The update
, replaces the local file with a copy of the remote one. The API includes onComplete and onError closures as parameters to keep your code loosely coupled.
When updatig a file, you may also provide an onProgress closure to can inform your users of the download progress. Or you can let them know they have up-to-date data.
It also supports copying a bundled file to the target location, as well, so your users don't have to download your app, then download the data next, before using your app.
Requirements
This pod has been written to work with Firebase Storage.
Currently, version is 3.1 (at time of writing)
pod 'Firebase/Storage', '~> 3.1'
Installation
FirebaseStorageCacheable is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'FirebaseStorageCacheable'
Usage
Import the library
import FirebaseStorageCacheable
Conform to the FirebaseStorageCacheable protocol
class MyCacheableFile: FirebaseStorageCacheable {
static var remotePath: String = "gs://remote/path/to/your/file.json"
static var targetPath: String = "/local/path/relative/to/your/apps/Documents/cached.json"
// optional parameter, used to locate a bundled copy, that can be copied to the targetPath
static var bundledFileName: String = "bundled.json"
}
Determine if the target file has been cached locally (either copy from the bundle or downloaded)
if MyCacheableFile.targetFileExists {
// target file exists
} else {
// target file does not exist
}
If you bundle a copy of the file with your app, copy it to the target location - if provided
MyCacheableFile.writeFromBundle(onComplete: {
// copy from bundle to app documents directory succeeded
},onError: { (error: FirebaseStorageCacheableError?) in
// handle error
})
Get the date of the last time the remote file was modified
MyCacheableFile.getRemoteModified(onComplete: { date in
// now you know when the remote file was updated last
}, onError: { error in
// handle error
})
Get the date of the last time the target file (local file) was modified
let date = MyCacheableFile.targetModified
Check to see if there is an updated version
This method compares the value from MyCacheableFile.getRemoteModified
and MyCacheableFile.targetModified
to determine whether or not an update is available
MyCacheableFile.checkForUpdate(onComplete: { (status: FirebaseStorageCacheableStatus) in
switch status {
case .updateAvailable:
// now would be a great time to trigger the MyCacheableFile.update method here
case .upToDate:
// you could display to the user they have up to date information
}
}, onError: { (error: FirebaseStorageCacheableError?) in
// handle error
})
Update the target file with the most recent version of the remote file
MyCacheableFile.update(onComplete: {
// the most recent version of the remote file has been downloaded and cached
}, onError: { (error: FirebaseStorageCacheableError?) in
// handle error
}, inProgress: { (fractionComplete: Double) in
// Update UI with download progress (this closure param optional)
})
Open the file and do your thing
if let filePath = MyCacheableFile.targetUrl, let contents = try? Data(contentsOf: filePath) {
// do your thing
} else {
// loading failed
}
License
FirebaseStorageCacheable is available under the MIT license. See the LICENSE file for more info.