在我的 Android 应用程序中,我使用 firebase-UI 中的 FirestoreRecyclerAdapter
它绑定(bind)Query
和Model
。当应用程序离线时,我需要它不显示任何内容。但应用程序显示来自缓存的数据,根据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.
和
最佳答案
如果您使用recyclerView.setHasFixedSize(true);
删除它,这对我有用...尝试一下。
关于java - 使用 Query for FirestoreRecyclerAdapter 时,我没有调用 get() 但需要设置 Query.get(Source.SERVER),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57884490/