10

I'm doing a simple query and periodically Firestore doesn't return anything. No error, no results, nothing.

Firestore.firestore().collection("groupChats")
        .order(by: kUpdatedAt, descending: true)
        .whereField("memberIds", arrayContains: currentUserId)
        .limit(to: 15).getDocuments { [weak self] snapshot, error in 
             // nothing inside here ever hits
        }

I'm not really sure how to proceed or debug this since this seems to be inside of Firestore. The user has an internet connection. Pulling to refresh and calling that query again returns the same seeming no-op result.

Any ideas what's going on here? Thanks!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Zack Shapiro
  • 6,648
  • 17
  • 83
  • 151
  • Firestore has offline data features, that are probably causing your problem. For Android and iOS, offline persistence is enabled by default. Have you checked where the data comes from using `snapshot.metadata.isFromCache`? – Sami Hult Dec 10 '18 at 16:00
  • How are those features causing this problem though? Not entirely sure how those two things relate? As I said in my post, the block never fires so I can't actually look at `snapshot.metadata.isFromCache` – Zack Shapiro Dec 10 '18 at 16:19
  • Right. I was hasty to judge the offline persistence. – Sami Hult Dec 10 '18 at 16:21
  • Your code comment says it never hits there, how do you know it never hits ? Do you log something ? Or have a breakpoint ? – Mostafa Berg Dec 12 '18 at 18:50
  • Yes, I have logs and breakpoints in the error and non-error cases and nothing prints or hits – Zack Shapiro Dec 12 '18 at 19:15
  • Does this problem happen with other quires to Firestore? Or just with this particular query?? – Ameer Taweel Dec 14 '18 at 19:07
  • It's happened with a number of queries – Zack Shapiro Dec 14 '18 at 19:37
  • Hasn't happened in the last week or so however – Zack Shapiro Dec 14 '18 at 19:46
  • Okay, just happened to me after I tried to add ~900 strings to an array. No writes don't seem to work – Zack Shapiro Dec 14 '18 at 20:03
  • The real answer is because beta. Firestore has been in beta for a while now and may remain beta for quite a while longer. GMail was in beta for years, if you remember. – trndjc Dec 15 '18 at 09:10
  • No, that's not the real answer, unfortunately. Gmail and Firestore are very different products built by different teams at different points in time. Their only similarities are that they're offered by google and both have a beta tag on them. Firestore works well for me 99% of the time and in this 1%, something is happening where a call doesn't return. Both times have occurred around a large get or set of data, so there's at least that breadcrumb there to go off of. Would love more data from a Firestore engineer or support rep – Zack Shapiro Dec 17 '18 at 15:12
  • Are you sure it has nothing to do with limits? (https://firebase.google.com/docs/database/usage/limits) The sdk usage limits the payload size (for instance) to 16mb instead of 256mb. Any chance you added monitoring (https://firebase.google.com/docs/analytics/ios/start) so you can inspect your loads? – ymz Dec 18 '18 at 07:10
  • Is your request timing out? – sschilli Dec 18 '18 at 20:37
  • I don't believe so; a timeout should at least return the error closure – Zack Shapiro Dec 18 '18 at 21:24
  • Have you tried logging your error or snapshot? – Gabriel Garrett Jan 03 '19 at 21:40
  • The completion block never returns so I can't log the error. Which makes this especially strange and tricky – Zack Shapiro Jan 03 '19 at 21:43
  • Is there any reproduction path? Do you have a minimum project which reproduces this behaviour? – J. Doe Jan 05 '19 at 15:34
  • Unfortunately not. I just have a few team members that bring this up periodically and I’m unable – Zack Shapiro Jan 05 '19 at 16:01
  • Unable to reproduce it – Zack Shapiro Jan 05 '19 at 16:01
  • Well. Seems that you have ruled most of the solutions. I saw something similar to your problem a couple of months ago and was driving me crazy. It only happened sometimes and there were not even errors to track. Searching through stack overflow I found this answer, which I gave it a try, and haven't seen it happening since then: https://stackoverflow.com/questions/46717898/firestore-slow-performance-issue-on-getting-data so probably you can try it. I know that snapshotListener is not ideal but at least it is a temporary solution until Firesbase guys fix the issue – Galo Torres Sevilla Jan 09 '19 at 22:37
  • IMHO your best shot is to enable verbose logging and try to pin down the issue see: https://firebase.google.com/docs/reference/swift/firebasefirestore/api/reference/Classes/Firestore#enablelogging_ You can also check this with Firebase support, they usually respond within a day or two. – Mosbah Jan 10 '19 at 15:35
  • Do you have the appropriate indexes for the queries that doesn't behave as expected? – Dennis Alund Mar 16 '19 at 00:46
  • The issue ended up being caching was turned on by default and I had a massive cache on the device. Turning the offline caching stuff off helped this significantly – Zack Shapiro Mar 18 '19 at 18:26
  • @ZackShapiro please try below my answer, don't forget to mark my answer as correct answer and to upwote if it works. Thanks – Faiz Fareed Mar 18 '19 at 19:51

3 Answers3

3

The issue ended up being caching is turned on by default and I had a massive cache on the device from development and migrations. Turning the offline caching off as part of the Firestore() initialization helped this significantly and made my app usable again.

Zack Shapiro
  • 6,648
  • 17
  • 83
  • 151
0

you should use following code

Firestore.firestore().collection("groupChats")
        .order(by: kUpdatedAt, descending: true)
        .whereField("memberIds", arrayContains: currentUserId)
        .limit(to: 15).getDocuments { (snapshot, error) in 
if error != nil{
            print("Error getting documents: \(error)")
        } else {
            for document in (snapshot?.documents)! {
                print("\(document.documentID) => \(document.data())")
            }
        }
Faiz Fareed
  • 1,498
  • 1
  • 14
  • 31
  • 1
    Thanks Faiz. There were a lot of comments above but I mentioned before that the completion block wasn't being hit at all. It wasn't returning an error or any data at all. The issue was a large cache on my device. I answered my own question that's actually the solution for my problem. Thanks – Zack Shapiro Mar 18 '19 at 20:16
0

I was having the same problem, for me the query field was named after a reserved word. val type:String="" i changed the reserved to something non-reserved and the queries are working like magic.

My code was like

val query=FirebaseFirestore.getInstance()
            .collection("Organizations")
            .orderBy("name")
            .whereEqualTo("type",orgType)

so i changed it to something more meaningful like the following.

val query=FirebaseFirestore.getInstance()
            .collection("Organizations")
            .orderBy("name")
            .whereEqualTo("organizationtype",orgType)
David Innocent
  • 606
  • 5
  • 16