5

I read when on Android in offline mode the onComplete() method will never return unless the device goes online so data can be persisted to the backen at Firestore.

When I'm in offline mode and do this get() I notice that it return with what I think must be local data since I have the setPersistenceEnabled(true)

 query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() { @Override public void onComplete(@NonNull Task<QuerySnapshot> task) { } }); 

I just have to ask if this is correct? If so is there some flag I can check if the data is online/offline data?

Sometimes doing this get() it does not return just hangs there forever, maybe that has to do with my device getting warm after heavy debugging.

1 Answer 1

15

Unlike in Firebase Realtime database where to enable offline persistence you should have used first, the following line of code:

FirebaseDatabase.getInstance().setPersistenceEnabled(true); 

In Cloud Firestore, for Android and iOS, offline persistence is enabled by default. So, there is no need to use: setPersistenceEnabled(true).

When you are offline and you are using a get() call, the result will be from the cached copy of the Cloud Firestore data that your app is actively using.

To check if the data is from cache or from Firestore servers, you can use the following line of code:

String source = querySnapshot.getMetadata().isFromCache() ? "Local Cache" : "Firebase Server"; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.