apollo-ios如何在磁盘上缓存数据?

时间:2019-02-15 10:26:36

标签: ios swift graphql 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'

enter image description here

1 个答案:

答案 0 :(得分:0)

当我使用CocoaPods 1.5.3时,在Apollo/Core之后缺少源pod install时,会导致错误。我已经通过将CocoaPods升级到1.6.0修复了它。

顺便说一句,最初的问题是如何使用ApolloSQLite?,因为我没有找到有关ApolloSQLite的任何文档。经过过多的搜索和查询后,我在下面列出了我的步骤,供其他人寻求帮助:

  1. 使用CocoaPods管理库:
pod 'Apollo'
pod 'Apollo/SQLite'
  1. 需要一个文件路径来保存用于缓存数据的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")
    }
    
  2. 配置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)