Apollo iOS Swift始终从本地缓存GraphQl获取

时间:2018-10-29 12:43:09

标签: ios swift graphql apollo cache-control

当我第一次使用Apollo处理每次获取的GraphQl API时,仅从服务器获取Apollo,否则总是从本地缓存获取

let apollo = ApolloClient(url: URL(string: graphQLEndpoint)!)
let meetingsQuery = AllMeetingsQuery()
apollo.fetch(query: meetingsQuery) { [weak self] result, error in
  guard let meetings = result?.data?.allMeetings else { return }
  print(conferences.count)
  self?.conferences = conferences.map {$0.fragments.meetingDetails }
}

1 个答案:

答案 0 :(得分:1)

每个服务器查询都可以通过 CachePolicy

进行控制

缓存策略,用于指定是从服务器获取结果还是从本地缓存加载结果。

public enum CachePolicy {
  /// Return data from the cache if available, else fetch results from the server.
  case returnCacheDataElseFetch
  ///  Always fetch results from the server.
  case fetchIgnoringCacheData
  /// Return data from the cache if available, else return nil.
  case returnCacheDataDontFetch
}

默认值为 returnCacheDataElseFetch ,这意味着Apollo从缓存中返回数据(如果可用),否则从服务器获取结果。

我通过使用 fetchIgnoringCacheData

更改cachePolicy解决了该问题
apollo.fetch(query: meetingsQuery ,cachePolicy: .fetchIgnoringCacheData)  { [weak self] result, error in
  guard let meetings = result?.data?.allMeetings else { return }
  print(conferences.count)
}