Search code examples
iosswiftgraphqlapollo-ios

apollo-ios How to cache data on disk?


I need cache my data on disk for offline view, I found ApolloSQLite in the apollo-ios source, but I can't find any documents of ApolloSQLite(I also tried Google search).

Environment:

Xcode 10.1
macOS 10.14.3
CocoaPods 1.5.3

Apollo (0.9.5)
SQLite.swift (0.11.5)

Too many errors coming after pod install:

pod 'Apollo'
pod 'Apollo/SQLite'

enter image description here


Solution

  • When I use CocoaPods 1.5.3, missing the source Apollo/Core after pod install, it causes the errors. I've fixed it by upgrade CocoaPods to 1.6.0.

    BTW, the original question is how to use ApolloSQLite? Because I've not found any documents for ApolloSQLite. After too many search & query, I list my steps as below for other people for help:

    1. use CocoaPods for manage the libs:
    pod 'Apollo'
    pod 'Apollo/SQLite'
    
    1. need a file path to save the sqlite file which use for cache data. Paste my codes for refer:

      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. configure the 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)