【问题标题】:Apollo iOS swift Always get from Local cache GraphQlApollo iOS swift 总是从本地缓存中获取 GraphQl
【发布时间】:2019-04-02 09:36:49
【问题描述】:

当我第一次使用 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 }
}

【问题讨论】:

    标签: ios swift graphql apollo cache-control


    【解决方案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)
    }
    

    【讨论】:

      猜你喜欢
      • 2021-07-22
      • 2021-08-08
      • 2018-10-16
      • 2018-07-14
      • 2021-08-24
      • 2020-10-22
      • 2020-07-07
      • 2018-03-20
      • 2016-11-27
      相关资源
      最近更新 更多