0

I was wondering if anyone had any ideas on how to query multiple strings from an array at once in 'Firebase'. Basically query like you would using an AND condition. I have looked into restructuring my data in a hundred different ways but nothing has worked for me. Also, I have too much data to dump all the data and then match to my array after the query is performed. Any help or suggestions are greatly appreciated.

var uniqueStoreId = [“1”, “2”, “3”, “4”, “5”, “6”]
var posts = [Post]()

ref.queryOrderedByChild("storeId").queryEqualToValue("\(uniqueStoreId)").observeEventType(.Value, withBlock: {snapshot in
                    if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {
                        for snap in snapshot{
                            if let postDict = snap.value as? Dictionary<String, AnyObject> {
                                let key = snap.key
                                let post = Post(postKey: key, postData: postDict)
                                self.posts.insert(post, atIndex: 0)
                            }
                        }
                    }

        })
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
kelsheikh
  • 1,278
  • 3
  • 17
  • 37
  • Firebase's querying capabilities only allow a single condition. Sometimes you can combine values in a way that allows a multi-value query, but that's not always the case. See [this answer on the topic](http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase). – Frank van Puffelen Aug 27 '16 at 16:31

1 Answers1

0

// do something like this, check whether its array or dictioary, then add in one array and then use as per requirement.. enjoy

                    var arrMessage = [MessageFirebase]()

                    if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {

                        //If its a Array then use below method
                        for snap in snapshots {
                            if let postDictionary = snap.value as? Dictionary<String, AnyObject> {
                                let key = snap.key
                                let message = MessageFirebase(key: key, dictionary: postDictionary)
                                // Items are returned chronologically, but it's more fun with the newest jokes first.

                                //if needed
                                arrMessage.insert(message, atIndex: 0)
                            }
                        }

                        //If its a dictionary then use below method
                        if !(arrMessage.count > 0) {
                            let key = snapshot.key
                            let message = MessageFirebase(key: key, dictionary: snapshot.value!)
                            // Items are returned chronologically, but it's more fun with the newest jokes first.

                            //if needed
                            arrMessage.insert(message, atIndex: 0)
                        }
                    }
Mehul
  • 3,033
  • 23
  • 37