ios - apollo-ios 如何在磁盘上缓存数据?
问题描述
我需要将我的数据缓存在磁盘上以供离线查看,我在 apollo-ios 源中找到了 ApolloSQLite,但我找不到 ApolloSQLite 的任何文档(我也尝试过 Google 搜索)。
环境:
Xcode 10.1
macOS 10.14.3
CocoaPods 1.5.3
Apollo (0.9.5)
SQLite.swift (0.11.5)
之后出现太多错误pod install
:
pod 'Apollo'
pod 'Apollo/SQLite'
解决方案
当我使用时,在之后CocoaPods 1.5.3
缺少源,会导致错误。我已经通过将 CocoaPods 升级到 1.6.0 来修复它。Apollo/Core
pod install
顺便说一句,最初的问题是如何使用 ApolloSQLite?因为我没有找到任何关于 ApolloSQLite 的文档。经过太多的搜索和查询,我将我的步骤列出如下,以寻求其他人的帮助:
- 使用 CocoaPods 管理库:
pod 'Apollo' pod 'Apollo/SQLite'
需要一个文件路径来保存用于缓存数据的 sqlite 文件。粘贴我的代码以供参考:
func temporarySQLiteFileURL() -> URL { let applicationSupportPath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).first! let dbPath = applicationSupportPath + "/com.[special name].cache" if !FileManager.default.fileExists(atPath: dbPath) { try? FileManager.default.createDirectory(atPath: dbPath, withIntermediateDirectories: true, attributes: nil) } let url = URL(fileURLWithPath: dbPath) return url.appendingPathComponent("db.sqlite3") }
配置 ApolloClient:
let configuration = URLSessionConfiguration.default configuration.timeoutIntervalForRequest = 10 let url = URL(string: self.graphQLApiAddress!)! let networkTransport = HTTPNetworkTransport(url: url, configuration: configuration) let cache = try? SQLiteNormalizedCache(fileURL: temporarySQLiteFileURL()) let store = ApolloStore(cache: cache ?? InMemoryNormalizedCache()) self.apolloClient = ApolloClient(networkTransport: networkTransport, store: store)
推荐阅读
- java - 如何在 Spring 中实现视频导出器?
- javascript - 如何保持 Material-UI 表格可滚动并具有动态高度?
- java - 如何通过 NFC 阅读器从 GooglePay/ApplePay 获取有效的信用卡号?
- java - DateTimeFormatter.parse() 更正日期信息而不是抛出异常
- python - 在附加到列表之前过滤字典
- c# - 我应该将什么传递给 SQLitePCL.raw.SetProvider() 以避免“'Microsoft.Data.Sqlite.SqliteConnection' 的类型初始化程序引发异常”
- python - 您是否有理由不能按列表中的元素/值名称使用 pop() 方法?
- eclipse - 怎么去掉这个红框
- angular - 泳道 ngx 折线图第二个 y 轴角度
- python - 混合数据类型的变压器