1

这是我用于缓存的 apollo 客户端代码

val apolloSqlHelper = ApolloSqlHelper.create(context,"my_app")
        val sqlNormalizedCacheFactory = SqlNormalizedCacheFactory(apolloSqlHelper)
        val cacheKeyResolver = object: CacheKeyResolver(){
            override fun fromFieldRecordSet(
                field: ResponseField,
                recordSet: MutableMap<String, Any>
            ): CacheKey {
                return if (recordSet["__typename"] == "Repository") {
                    CacheKey.from(recordSet["id"] as String)
                } else {
                    CacheKey.NO_KEY
                }
            }

            override fun fromFieldArguments(
                field: ResponseField,
                variables: Operation.Variables
            ): CacheKey {
                return  CacheKey.NO_KEY
            }
        }

        ApolloClient.builder().serverUrl(baseUrl)
            .normalizedCache(sqlNormalizedCacheFactory,cacheKeyResolver)
            .okHttpClient(okHttpClient).build()

我正在做一个查询,之后阿波罗缓存了我的结果,但是当更多数据添加到查询中时,我不明白如何根据结果是否更改来使缓存无效。如果没有结果更改从缓存中获取数据或获取新数据并更新缓存

4

1 回答 1

0

我不需要手动使缓存无效。Apollo Android 为我处理

这段代码对我有用

apolloClient.query(MyQuery()).responseFetcher(ApolloResponseFetchers.NETWORK_FIRST)

ApolloResponseFetchers还提供各种选项CACHE_FIRST,例如CACHE_ONLY

于 2020-06-08T07:34:12.600 回答