Search code examples
iosswiftfirebasegeofire

Firebase query using a list of ids (iOS)


I have an NSArray containing multiple ids. Is there a way in Firebase where I can get all the object with the ids in the array?

I am building a restaurant rating app which uses GeoFire to retrieve nearby restaurants. My problem is that GeoFire only returns a list of ids of restaurant that are nearby. Is there any way i can query for all the object with the ids?


Solution

  • No, you can't do a batch query like that in Firebase.

    You will need to loop over your restaurant IDs and query each one using observeSingleEvent. For instance:

    let restaurantIDs: NSArray = ...
    let db = FIRDatabase.database().reference()
    for id in restaurantIDs as! [String] {
        db.child("Restaurants").child(id).observeSingleEvent(of: .value) {
            (snapshot) in
            let restaurant = snapshot.value as! [String: Any]
            // Process restaurant...
        }
    }
    

    If you are worried about performance, Firebase might be able to group all these observeSingleEvent calls and send them as a batch to the server, which may answer your original question after all ;-)