FirebaseStorageCacheable 0.1.2

FirebaseStorageCacheable 0.1.2

Jared Anderton 维护。



  • 作者
  • jaredanderton

FirebaseStorageCacheable

CI Status Version License Platform

概述

FirebaseStorageCacheable 是一个库,可以下载并缓存文件的最新版本 Firebase Storage

此库允许您下载和缓存托管在 Firebase Storage 中的文件。

它使用您本地文件的最后修改时间戳,与远程文件的最后修改时间戳(通过使用文件元信息)进行比较,以确定远程文件是否较新。

update 方法会使用远程文件的一个副本替换本地文件。API 包括 onComplete 和 onError 作为参数,以保持代码松耦合。

当更新文件时,您还可以提供一个 onProgress 回调函数,以通知用户下载进度。或者,您可以通知他们数据是最新的。

它还支持将打包文件复制到目标位置,因此您的用户不必先下载应用程序,然后再下载数据。

要求

该 Pod 已编写为与 Firebase Storage 一起工作。

目前版本为 3.1(撰写本文时)

pod 'Firebase/Storage', '~> 3.1'

安装

FirebaseStorageCacheable 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中

pod 'FirebaseStorageCacheable'

使用方法

导入库

import FirebaseStorageCacheable

遵守 FirebaseStorageCacheable 协议

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"
}

确定目标文件是否已在本地缓存(无论是从捆绑包复制还是已下载)

if MyCacheableFile.targetFileExists {
    // target file exists
} else {
    // target file does not exist
}

如果您将文件的副本捆绑到您的应用程序中,请将其复制到目标位置 - 如果提供了的话

MyCacheableFile.writeFromBundle(onComplete: {
    // copy from bundle to app documents directory succeeded
},onError: { (error: FirebaseStorageCacheableError?) in
    // handle error
})

获取远程文件上次修改的日期

MyCacheableFile.getRemoteModified(onComplete: { date in
    // now you know when the remote file was updated last
}, onError: { error in
    // handle error
})

获取目标文件(本地文件)最后修改日期

let date = MyCacheableFile.targetModified

检查是否有更新版本

此方法通过比较MyCacheableFile.getRemoteModifiedMyCacheableFile.targetModified的值来决定是否有更新可用。

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
})

使用远程文件的最新版本更新目标文件

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)
})

打开文件并执行操作

if let filePath = MyCacheableFile.targetUrl, let contents = try? Data(contentsOf: filePath) {
    // do your thing
} else {
    // loading failed
}

许可协议

FirebaseStorageCacheable遵循MIT许可证。有关更多信息,请参阅LICENSE文件。