0

In my android app I am using FirestoreRecyclerAdapter from firebase-UI which binds Query and Model. When app is off-line I need it to show nothing. But the app shows data from cache which is DEFAULT according to docs

I also tried to .setPersistenceEnabled(false) on FirestoreRecyclerOptions which didn't help.

From this I figured out that I need to use get(Source.SERVER) on Query but I am not using get() on my query as I am using it in FirestoreRecyclerOptions, which is used by FirestoreRecyclerAdapter

Here is my code which is basically same as in example app from firebaseui in lines 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);

When I disconnect the Internet I expect to see empty list, but instead I see this log:

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.

and

not in sync screenshot

Community
  • 1
  • 1
Koby
  • 150
  • 2
  • 13
  • How did you tried to use `.setPersistenceEnabled(false)` and it didn't work? – Alex Mamo Sep 11 '19 at 08:08
  • Not sure if I did it right but I put the following code right before the code in the post: FirebaseFirestore db = FirebaseFirestore.getInstance(); FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder() .setPersistenceEnabled(false) .build(); db.setFirestoreSettings(settings); FirebaseFirestore.getInstance().setFirestoreSettings(settings); – Koby Sep 11 '19 at 08:11
  • It's right but what happened when you did this? – Alex Mamo Sep 11 '19 at 08:12
  • When I disconnect AVD from the Internet it still shows some items from the list. Also I am able to check/uncheck Checkboxes while my code changes state in firebase server. I should see that checkbox is changed its state ONLY after server changed it state of particular checkbox. So what I expect is that UI fully depend on server state or shows nothing otherwise – Koby Sep 11 '19 at 08:28
  • Have you tried to clear the cache, use `.setPersistenceEnabled(false)` and read the data again? Does it work this way? – Alex Mamo Sep 11 '19 at 08:48
  • I just tried to use FirebaseFirestore.getInstance().clearPersistence();. For few seconds same behavior as before, part of items shown and checkable checkboxes, then application crashes saying "failed to connect to firestore.googleapis.com" – Koby Sep 11 '19 at 10:28

2 Answers2

0

If you are using recyclerView.setHasFixedSize(true); remove it this is working for me...just give it try.

Riad Baghbanli
  • 3,105
  • 1
  • 12
  • 20
0

Try using adapter.startListening() and adapter.stopListening();