gpt4 book ai didi

ios - Apollo iOS swift 总是从本地缓存中获取 GraphQl

转载 作者:行者123 更新时间:2023-11-28 10:37:03 24 4
gpt4 key购买 nike

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

关于ios - Apollo iOS swift 总是从本地缓存中获取 GraphQl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53045765/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com