0

I've read several articles here on StackOverflow asking this same question and the answers are all the same but it doesn't seem to work for me and I'm not sure why. I've read Elegant Way To Dynamically Query FireStore, Conditional Where Query on Firestore, the two articles that question references, plus How to conditionally add another filter to a query in Firebase Firestore using swift?, and the article Firestore Query Options Swift.

I followed the simple guidance the all offered:

var ref = db.collection("Violations")

if locationCompoundQuery != "" {
            ref = ref.whereField("location", isEqualTo: locationCompoundQuery)
        }

The issue is that Xcode gives me an error: Cannot assign value of type 'Query' to type "CollectionReference" insert 'as! CollectionReference'. If I do that it will compile and run, but when the query runs it crashes. The message in the console is: Could not cast value of type 'FIRQuery' (0x108972150) to 'FIRCollectionReference'

I'm using Xcode 13.1. All of my other queries that are compound without conditional statements run just fine. Any help is greatly appreciated.

JimD
  • 1
  • 1
  • Well, the error is on point. `ref` is a reference to a collection and `ref.whereField` is a query. Try `let query = ref.whereField("location", isEqualTo: locationCompoundQuery)`. Not sure what this `locationCompoundQuery` resolves to but that could be a problem as well. If that doesn't fix it, update your question with more complete information – Jay Jan 27 '22 at 21:17

1 Answers1

0

I had the same issue. This works for me on Xcode 14.0.1 and Firebase 9.6.0.

            let ref = db.collection(EventsObject)
            var query: Query = ref
            if filter == .all {
                query = ref.whereField(CloudEvent.CodingKeys.ownerID.stringValue, isNotEqualTo: "")         // must have any OwnerID
            } else if filter == .allPublished {
                query = ref.whereField(CloudEvent.CodingKeys.ownerID.stringValue, isNotEqualTo: "")         // must have any OwnerID
                query = query.whereField(CloudEvent.CodingKeys.published.stringValue, isEqualTo: true)      // and has been published
            } else if filter == .onlyThisUser {
                query = ref.whereField(CloudEvent.CodingKeys.ownerID.stringValue, isEqualTo: currentUser)   // belongs to this user
            } else if filter == .onlyThisUserPublished {
                query = ref.whereField(CloudEvent.CodingKeys.ownerID.stringValue, isEqualTo: currentUser)   // belongs to this user
                query = query.whereField(CloudEvent.CodingKeys.published.stringValue, isEqualTo: true)      // and has been published
            } else if filter == .onlyThisUserNotPublished {
                query = ref.whereField(CloudEvent.CodingKeys.ownerID.stringValue, isEqualTo: currentUser)   // belongs to this user
                query = query.whereField(CloudEvent.CodingKeys.published.stringValue, isEqualTo: false)     // and has not been published
            }
            
            query.addSnapshotListener({ querySnapShot, error in
                if let querySnapShot = querySnapShot {
                    self.cloudEvents = querySnapShot.documents.compactMap { document in
                        do {
                            return try document.data(as: CloudEvent.self)
                        }
                        catch {
                            EventLogging.logger.error("error loading cloud events: \(error.localizedDescription)")
                        }
                        return nil
                    }
                }
            })
Mark Jackson
  • 83
  • 1
  • 7