1

在此处关注 Apollo Android 中的 Support Caching 之后:Support For Cached Responses for Android

并且已经尝试离线查询并且工作正常并且数据来自缓存

  • 打开应用程序后的第一个场景是使用“MyApolloClient”对象的查询加载数据列表,该对象是从“ApolloClient”继承的自定义类

  • 使用“.watcher().enqueueAndWatch()”方法获取数据后,使用新数据回调来自 Apollo 的响应并更新 recyclerView

  • 我从查询数据列表中选择要更新或“变异”记录的项目,以在缓存和数据库中进行更改。

我可以单击任何项​​目,然后它会显示一个对话框来编辑该记录项目的名称

使用正常发送更新后的名称后Mutation,缓存发生了变化,在线数据库也发生了变化。

这里的问题: UI 根本没有改变,因为.watcher()返回实例的ApolloQueryWatcher方法enqueueAndWatch 不会触发作为侦听器来更新 UI 和具有该记录的新更改值的列表的任何更改

这是我的代码中的一些片段,我使用的是 Kotlin 和 Java。

  • 使用Query从数据库中获取用户数据的方法

    fun getAllUsers() {
    progress.visibility = VISIBLE
    MyApolloClient.getApolloClient(this).query(GetUsersWithPagesQuery.builder().build())
        .watcher().enqueueAndWatch(object : MyApolloClient.MyCallBack<GetUsersWithPagesQuery.Data>(this) {
            override fun onResponseUI(response: Response<GetUsersWithPagesQuery.Data>) {
                progress.visibility = GONE
                if (response.data() != null) {
                    reachBottom = false
                    users.clear()
                    for (i in response.data()!!.users()) {
                        val u = GetUsersWithPagesQuery.Data1("", i.id(), i.name(), i.email())
                        users.add(u)
                    }
                    adapter.notifyDataSetChanged()
    
                }
            }
    
            override fun onFailureUI(response: ApolloException) {
    
            }
        })
    

    }

  • 自定义回调以在 UI 中使用响应

    public static abstract class MyCallBack<T> extends ApolloCall.Callback<T> {
    
    Activity activity;
    
    public MyCallBack(Activity activity) {
        this.activity = activity;
    }
    
    @Override
    public void onResponse(@NotNull com.apollographql.apollo.api.Response<T> response) {
        activity.runOnUiThread(() -> onResponseUI(response));
    
    }
    
    public abstract void onResponseUI(@NotNull com.apollographql.apollo.api.Response<T> response);
    
    public abstract void onFailureUI(@NotNull ApolloException response);
    
    @Override
    public void onFailure(@NotNull ApolloException e) {
        activity.runOnUiThread(() -> onFailureUI(e));
    }
    

    }

  • 获取具有缓存支持的 Apollo Client 对象

    public static ApolloClient getApolloClient(Context context) {
    
    HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
    httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    builder.addNetworkInterceptor(httpLoggingInterceptor);
    builder.addNetworkInterceptor(chain -> {
        Request original = chain.request();
        Request.Builder builder1 = original.newBuilder().method(original.method(), original.body());
        builder1.header("Authorization", "Bearer " + PrefManager.getInstance(context).getAPIToken());
        return chain.proceed(builder1.build());
    });
    OkHttpClient okHttpClient = builder.build();
    //Directory where cached responses will be stored
    File file = new File(context.getApplicationContext().getFilesDir(), "apolloCache");
    
    //Size in bytes of the cache
    long size = 1024 * 1024;
    
    //Create the http response cache store
    DiskLruHttpCacheStore cacheStore = new DiskLruHttpCacheStore(file, size);
    
    ApolloSqlHelper apolloSqlHelper = ApolloSqlHelper.create(context, "zari");
    
    //Create NormalizedCacheFactory
    NormalizedCacheFactory cacheFactory = new SqlNormalizedCacheFactory(apolloSqlHelper);
    
    //Create the cache key resolver, this example works well when all types have globally unique ids.
    CacheKeyResolver resolver = new CacheKeyResolver() {
        @NotNull
        @Override
        public CacheKey fromFieldRecordSet(@NotNull ResponseField field, @NotNull Map<String, Object> recordSet) {
            return formatCacheKey((String) recordSet.get("id"));
        }
    
        @NotNull
        @Override
        public CacheKey fromFieldArguments(@NotNull ResponseField field, @NotNull Operation.Variables variables) {
            return formatCacheKey((String) field.resolveArgument("id", variables));
        }
    
        private CacheKey formatCacheKey(String id) {
            if (id == null || id.isEmpty()) {
                return CacheKey.NO_KEY;
            } else {
                return CacheKey.from(id);
            }
        }
    };
    
    apolloClient = ApolloClient.builder()
            .serverUrl(url)
            //.httpCache(new ApolloHttpCache(cacheStore))
            .normalizedCache(cacheFactory, resolver)
            .okHttpClient(okHttpClient)
            .build();
    return apolloClient;
    

    }

  • 带有编辑文本的警报对话框以更新 Recycler Adapter 列表中的项目

    builder.setPositiveButton("Change", (dialog, which) -> {
        if (!n.isEmpty())
            MyApolloClient.getApolloClient(activity).mutate(UpdateUserMutation.builder().id(user
                    .id()).name(n).build()).enqueue(new MyApolloClient.MyCallBack<UpdateUserMutation.Data>(activity) {
                @Override
                public void onResponseUI(@NotNull Response<UpdateUserMutation.Data> response) {
                    if (response.errors() != null && response.errors().size() > 0) {
                        StaticMembers.toastMessageShort(activity, response.errors().get(0).message());
                    } else {
                        StaticMembers.toastMessageShort(activity, response.data().updateUser().name() + " updated");
                    }
                }
    
                @Override
                public void onFailureUI(@NotNull ApolloException response) {
    
                }
            });
    
    });
    

那么,有人可以帮忙吗?

4

1 回答 1

0

Apollo-android 无法知道您的突变将修改哪个节点。如果您的 API 允许,您可以查询新用户作为突变的返回数据,以强制更新缓存。

或者,如果请求成功,您可以手动修改缓存,但这似乎更复杂。

于 2019-08-14T15:15:11.353 回答