Tarvo Mäesepp
Tarvo Mäesepp

Reputation: 4533

Getting key values that belongs to child with Firebase

I am trying to get values from Firebase that belongs to other child. For example lets take this part of my JSON:

{
  "Snuses" : {
      "CATCH EUCALYPTUS WHITE LARGE" : {
          "Brand" : "Catch",
          "Products" : "CATCH EUCALYPTUS WHITE LARGE",
          "Some property" : "21.6",
        },
        "CATCH DRY EUCALYPTUS WHITE MINI": {
          "Brand" : "Catch",
          "Products" : "CATCH DRY EUCALYPTUS WHITE MINI",
          "Some property" : "5.4",

        }
      "CRAFTED KARDUS HIGHLAND SINGLE CUT LOOSE" : {
        "Brand" : "Crafted Snus",
          "Products" : "CRAFTED KARDUS HIGHLAND SINGLE CUT LOOSE",
          "Some property" : "32.0",
        },
  "Brands": {
    "CATCH":0,
    "CRAFTED SNUS":0
    }
  }
}

I've done the easiest part, queried all the "Brands" into tableview. Now I want that if I click for example on "CATCH" to see other tableview with all its "Products". I had it using dictionary but is it possible other way? I think I am missing the logic here.

I've detected even the clicked cell and done the segue using this code:

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return brands.count

    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("snusBrandsCell")
        if let name = brands[indexPath.row] as? String{
            let snusBrandLabel = cell?.contentView.viewWithTag(1) as! UILabel
            snusBrandLabel.text = name



        }
        return cell!
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("products at \(indexPath.row)  --> \(brands[indexPath.row])")


        if let products = brands[indexPath.row] as? [[String:String]]{
            valueTopass = products

            performSegueWithIdentifier("toProducts", sender: self)
        }
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){

        if (segue.identifier == "toProducts") {
            var snusProductsView = segue.destinationViewController as! SnusProductsTableViewController
            snusProductsView.productsValue = self.valueTopass
            print(self.valueTopass)

        }
    }

}

Upvotes: 1

Views: 243

Answers (1)

Tarvo Mäesepp
Tarvo Mäesepp

Reputation: 4533

I must be dumb. First of all I needed to set ".indexOn": "". And the code that worked:

 FIRDatabase.database().reference().child("Snuses").queryOrderedByChild("Brand").queryEqualToValue(brands[indexPath.row])
.observeSingleEventOfType(.ChildAdded, withBlock: { snapshot in
print(snapshot.key)
})

Everytime gotta read docs before asking :) Hope it helps someone.

I got the answer thanks to @DimaRostopira

Upvotes: 2

Related Questions