question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

[help wanted] Firestore will not return a response after a long time since you started using it.

See original GitHub issue

Overview (Required)

  • We can not use favorite!! šŸ˜­
  • I can fix it clear app data. So probably, app internal problem.
  • I canā€™t fix this by force stop app.
  • It occurred release and debug
  • If it occurred, FavoriteFirestoreDatabase ā€œFirestore:setupFavoritesDocumentā€ is logged, But ā€œFirestore:setupFavoritesDocument onCompleteā€ is not logged.

https://github.com/DroidKaigi/conference-app-2018/blob/master/app/src/main/java/io/github/droidkaigi/confsched2018/data/db/FavoriteFirestoreDatabase.kt#L100

    @CheckResult
    private fun setupFavoritesDocument(currentUser: FirebaseUser): Single<FirebaseUser> {
        return Single.create({ e: SingleEmitter<FirebaseUser> ->
            if (DEBUG) Timber.d("Firestore:setupFavoritesDocument")
            val database = FirebaseFirestore.getInstance()
            val favorites = database.collection("users/" + currentUser.uid + "/favorites")
            favorites.get().addOnCompleteListener { task ->
                if (DEBUG) Timber.d("Firestore:setupFavoritesDocument onComplete")

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

3reactions
kakajikacommented, Jan 26, 2018

Hello šŸ™‹ First comment on this project!

I think itā€™s a problem about Firestoreā€™s behavior when device is regarded as offline.

The official document of Firestore says:

If you get a document while the device is offline, Cloud Firestore returns data from the cache. If the cache does not contain data for that document, or the document does not exist, the get call returns an error.

https://firebase.google.com/docs/firestore/manage-data/enable-offline#get_offline_data

So, if the document is empty, favorites.get() cause error and return nothing. We should use favorites.addSnapshotListener(...) instead.

    @CheckResult
    private fun setupFavoritesDocument(currentUser: FirebaseUser): Single<FirebaseUser> {
        val database = FirebaseFirestore.getInstance()
        val favorites = database.collection("users/" + currentUser.uid + "/favorites")

        return Single.create({ e: SingleEmitter<QuerySnapshot> ->
            if (DEBUG) Timber.d("Firestore:setupFavoritesDocument")
            val listener = favorites.addSnapshotListener { querySnapshot, exception ->
                if (DEBUG) Timber.d("Firestore:setupFavoritesDocument onComplete")
                if (exception != null) {
                    Timber.e(exception, "Firestore:setupFavoritesDocument onComplete fail ")
                    e.onError(exception)
                } else {
                    e.onSuccess(querySnapshot)
                }
            }
            e.setCancellable {
                listener.remove()
            }

        // To avoid [ get -> write -> get -> ... ] loop, make task split.
        }).flatMap { querySnapshot ->
            Single.create<FirebaseUser> { e ->
                if (querySnapshot.isEmpty) {
                    favorites.add(mapOf("initialized" to true)).addOnCompleteListener {
                        if (DEBUG) Timber.d("Firestore:create document for listing")
                        e.onSuccess(currentUser)
                    }
                } else {
                    if (DEBUG) Timber.d("Firestore:document already exists")
                    e.onSuccess(currentUser)
                }
            }
        }
    }

I tried code above, and it seems to work well. If Itā€™s OK, I create PR! šŸ˜ƒ

3reactions
Kesin11commented, Jan 24, 2018

I did reproduce this issue may be. In my case, same behaviour occurs when launch app without network connection.

  1. Turn off wifi
  2. Launch app
  3. Turn on wifi
  4. Touch favorite icon in AllSessionsFragment. Icon is turned on
  5. Go to session detail
  6. Favorite icon is still turned off

I tried to repeat step 4 to 6 after turned on wifi, it still same result. But after about 1 minutes, it revived suddenly. However, I can not see any log in setupFavoritesDocument()

Read more comments on GitHub >

github_iconTop Results From Across the Web

Best practices for Cloud Firestore - Firebase
If you are not querying based on a large array or map field, you should exempt it from indexing. Read and write operations....
Read more >
ā€œWhy is my Cloud Firestore query slow?ā€ | Firebase Developers
The best way to fix this issue is to make sure you're not transferring down more data than you need. One simple option...
Read more >
Getting data | Firestore - Google Cloud
There are three ways to retrieve data stored in Firestore. Any of these methods can be used with documents, collections of documents, or...
Read more >
firebase - Firestore - How to get document id after adding a ...
Yes it is possible. When you call the .add method on a collection, a DocumentReference object is returned. DocumentReference has the idĀ ...
Read more >
Cloud Firestore - React Native Firebase
If you have started to receive a app:mergeDexDebug error after adding Cloud Firestore, please read the Enabling Multidex documentation for more information onĀ ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found