apollo-ios 如何在磁盘上缓存数据?
apollo-ios How to cache data on disk?
我需要在磁盘上缓存我的数据以供离线查看,我在 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
时,在 pod install
之后缺少来源 Apollo/Core
,这会导致错误。我通过将 CocoaPods 升级到 1.6.0 来修复它。
顺便说一句,最初的问题是 如何使用 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)
我需要在磁盘上缓存我的数据以供离线查看,我在 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
时,在 pod install
之后缺少来源 Apollo/Core
,这会导致错误。我通过将 CocoaPods 升级到 1.6.0 来修复它。
顺便说一句,最初的问题是 如何使用 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)