java - 使用 Query for FirestoreRecyclerAdapter 时,我没有调用 get() 但需要设置 Query.get(Source.SERVER)

标签 java android firebase google-cloud-firestore firebaseui

在我的 Android 应用程序中,我使用 firebase-UI 中的 FirestoreRecyclerAdapter它绑定(bind)QueryModel。当应用程序离线时,我需要它不显示任何内容。但应用程序显示来自缓存的数据,根据docs,这是DEFAULT

我还尝试在 FirestoreRecyclerOptions 上使用.setPersistenceEnabled(false),但没有帮助。

来自this我发现我需要在 Query 上使用 get(Source.SERVER) 但我没有在 上使用 get()查询,因为我在 FirestoreRecyclerOptions 中使用它,它由 FirestoreRecyclerAdapter

使用

这是我的代码,与 example app 中的代码基本相同来自 firebaseui 第 160-178 行。

Query query = FirebaseFirestore.getInstance()
                .collection("files")
                .orderBy("timestamp")
                .limit(50);

        FirestoreRecyclerOptions<FileToPrint> options = new FirestoreRecyclerOptions.Builder<FileToPrint>()
                .setQuery(query, FileToPrint.class)
                .build();

        adapter = new FirestoreRecyclerAdapter<FileToPrint, FileToPrintHolder>(options) {
            @Override
            public void onBindViewHolder(FileToPrintHolder holder, int position, FileToPrint model) {
                // Bind the Chat object to the ChatHolder
                holder.bind(model);
            }

            @Override
            public FileToPrintHolder onCreateViewHolder(ViewGroup group, int i) {
                // Create a new instance of the ViewHolder, in this case we are using a custom
                // layout called R.layout.message for each item
                View view = LayoutInflater.from(group.getContext())
                        .inflate(R.layout.file_to_print_item, group, false);

                return new FileToPrintHolder(view);
            }
        };
        mRecyclerView.setAdapter(adapter);

当我断开互联网连接时,我希望看到空列表,但我看到的是以下日志:

Firestore: (21.0.0) [OnlineStateTracker]: Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds

This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.

not in sync screenshot

最佳答案

如果您使用recyclerView.setHasFixedSize(true); 删除它,这对我有用...尝试一下。

关于java - 使用 Query for FirestoreRecyclerAdapter 时,我没有调用 get() 但需要设置 Query.get(Source.SERVER),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57884490/

相关文章:

android - 在 Android 版 Firebase 3 中更改用户的电子邮件/密码时出现错误

android - 在模型 Firebase 上保存 userId

java - 如何将 Netbeans 项目导入 Eclipse

java - 尝试将 float 转换为 int 时遇到问题

java - 安全地关闭从不同线程读取套接字输入流的线程

java - 如何从作为参数传递给方法的 JList 中删除元素?

android - React Native 和 JSON 文件解析

Android:我应该使用哪种图像格式,为什么?

android - 移动应用专用内存分配

ios - 用户更改密码时 Firebase 注销所有帐户