ios - Apollo iOS swift 总是从本地缓存中获取 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 {$0.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 从缓存中返回数据(如果可用),否则从服务器获取结果。
我通过使用fetchIgnoringCacheData更改 cachePolicy 解决了这个问题
apollo.fetch(query: meetingsQuery ,cachePolicy: .fetchIgnoringCacheData) { [weak self] result, error in
guard let meetings = result?.data?.allMeetings else { return }
print(conferences.count)
}
推荐阅读
- java - 将文件上传到 Firebase 存储时,RecyclerView 中的进度条未更新
- azure-pipelines - 在 Yarn 中设置私有注册表不起作用
- vb.net - 如何获取源代码并将其放入我的 gridview 中?
- javascript - AsyncStorage 已从 react-native 核心中提取,将在未来的版本中删除
- android - 与 Playstore aab 不同的独立 apk:LinearGradient 使用错误的颜色
- unity3d - 使用 WebGL 构建 Unity3D 项目时的“资源文件”是什么?使用 Unity3D WebGL-Build 需要考虑什么?
- firebase - 使用firestore访问的React-native-firebase和firebase配置问题
- java - 确定近似根的函数
- java - java.lang.NoClassDefFoundError:无法初始化类 org.codehaus.groovy.runtime.InvokerHelper
- python - 使用临时字典在 For 循环中收集数据 | 硒 | Python