Apollo iOS swift 始终从本地缓存 GraphQl 中获取
Apollo iOS swift Always get from Local cache GraphQl
当我第一次使用 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 {[=12=].fragments.meetingDetails }
}
来自服务器的每个查询都可以由 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 return 从缓存中获取数据(如果可用),否则从服务器获取结果。
我通过使用 fetchIgnoringCacheData
更改 cachePolicy 解决了这个问题
apollo.fetch(query: meetingsQuery ,cachePolicy: .fetchIgnoringCacheData) { [weak self] result, error in
guard let meetings = result?.data?.allMeetings else { return }
print(conferences.count)
}
当我第一次使用 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 {[=12=].fragments.meetingDetails }
}
来自服务器的每个查询都可以由 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 return 从缓存中获取数据(如果可用),否则从服务器获取结果。
我通过使用 fetchIgnoringCacheData
更改 cachePolicy 解决了这个问题apollo.fetch(query: meetingsQuery ,cachePolicy: .fetchIgnoringCacheData) { [weak self] result, error in
guard let meetings = result?.data?.allMeetings else { return }
print(conferences.count)
}