Guille Acosta
Guille Acosta

Reputation: 2222

querying firebase data among all children with auth

I followed this tutorial to make an app with firebase sync and authentication. But now, I don't know how to make a query to search among all children because these children have a lexicographical-based key.

To be more specific, from this example, how do you query all "currentCity" from all "users"

{
  "users" : {
    "1e2f048f-a3a0-4190-afad-81d724ed1997" : {
      "currentCity" : "Arrecifes",
      "currentCountry" : "ARG",
      "currentState" : "BSA",
      "day" : {
        "domingo" : true,
        "jueves" : true,
        "martes" : false,
        "miercoles" : true
      },
      "gender" : "Masculino",
      "name" : "Guillermo H Acosta",
      "position" : "Medio",
      "registered" : true,
      "timeSince" : "22:00",
      "timeUntil" : "23:00",
      "whereToPlay" : "Güemes"
    },
    "39c6ccf9-61ec-446e-9af3-3a87810fab71" : {
      "currentCity" : "Alvear",
      "currentCountry" : "ARG",
      "currentState" : "CRR",
      "day" : {
        "jueves" : true,
        "miercoles" : true,
        "viernes" : true
      },
      "gender" : "Masculino",
      "name" : "Guillermo Acosta",
      "registered" : true,
      "timeSince" : "21:00",
      "timeUntil" : "23:00"
    },
    "4991bdc9-dfc4-4ff5-ab81-0e28f1b3ab53" : {
      "currentCity" : "Cordoba",
      "currentCountry" : "ARG",
      "currentState" : "COR",
      "day" : {
        "miercoles" : true,
        "viernes" : true
      },
      "gender" : "Masculino",
      "name" : "Hsjja",
      "timeSince" : "02:00",
      "timeUntil" : "03:00"
    },
    "509364fd-388b-42ff-91ed-0811811a4ff3" : {
      "day" : {
        "jueves" : true,
        "lunes" : true,
        "martes" : false,
        "sabado" : true
      },
      "name" : "Guillermo Acosta",
      "position" : "Delantero",
      "timeSince" : "13:00",
      "timeUntil" : "14:00",
      "whereToPlay" : "Güemes"
    }
  }
}

Upvotes: 0

Views: 132

Answers (2)

Logan Jahnke
Logan Jahnke

Reputation: 815

This will parse through each node grabbing the currentCity key. If currentCity is NULLABLE, I would recommend housing it in a separate node.

let ref = // path to users here
ref.observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) in
    // Put all of the users into a dictionary
    let postDict = snapshot.value as! [String : AnyObject]
    for user in postDict {
        // Set each child of the user as a dictionary
        let data = activity.1 as! [String : AnyObject]

        // Get the specific child
        let currentCity = children["currentCity"] as! String

        // Do something with the data
        print(currentCity)
    }
})

Upvotes: 0

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

With the Firebase Database, you either get a node or you don't get that node. There is no way to get a part of each nodes in a query.

So if you want load just the cities of all users, you should keep a node that contains just that information:

{
  "userCurrentCities" : {
    "1e2f048f-a3a0-4190-afad-81d724ed1997" : "Arrecifes",
    "39c6ccf9-61ec-446e-9af3-3a87810fab71" : "Alvear",
    "4991bdc9-dfc4-4ff5-ab81-0e28f1b3ab53" : "Cordoba",
  }
}

Upvotes: 1

Related Questions