0

I've spend hours looking at identical questions but none of the answers I've found are helping this issue. Simple app retrieves data from Firebase Database and passes to another view controller from the tableview. The main data will pass through but I can't edit the information without an identifying "key" which I tried to set as childByAutoID() but then changed to a timestamp. Regardless of the method, all I get is the entries info not the actual key itself.

func loadData() {
    self.itemList.removeAll()
    let ref = FIRDatabase.database().reference()


    ref.child(userID!).child("MyStuff").observeSingleEvent(of: .value, with: { (snapshot) in



        if let todoDict = snapshot.value as? [String:AnyObject] {
            for (_,todoElement) in todoDict {



                let todo = TheItems()



                todo.itemName = todoElement["itemName"] as? String
                todo.itemExpires = todoElement["itemExpires"] as? String
                todo.itemType = todoElement["itemType"] as? String



                self.itemList.append(todo)

                print (snapshot.key);

            }



        }

        self.tableView.reloadData()

    }) { (error) in
        print(error.localizedDescription)
    }


}
Sarah Aziziyan
  • 498
  • 9
  • 22
  • show picture database and what key you want to retrieve – J. Doe Sep 05 '17 at 12:51
  • Possible duplicate of [Firebase snapshot.key not returning actual key?](https://stackoverflow.com/questions/37581193/firebase-snapshot-key-not-returning-actual-key) – DoesData Sep 05 '17 at 12:57
  • Additionally, you could just add the key into your data when you upload it. So you could do let key = todoElement["key"] as? String – DoesData Sep 05 '17 at 12:58
  • under the uid/"MyStuff"/ there is a childbyAutoID and then itemName/ItemExpires/ItemType. I'm trying to retrieve the childByAutoId number but .key returns "MyStuff" – Bradley Carter Sep 05 '17 at 13:10

2 Answers2

0

The key you are trying to look for is located in the iterator of your for loop

Inside your if-let, try to do this:

for (key,todoElement) in todoDict {
    print(key) // this is your childByAutoId key 
}

This should solve the problem. Otherwise show us a screen of your database structure

Giuseppe Sapienza
  • 4,101
  • 22
  • 23
0

If your data looks like this:

Uid: {
    MyStuff: {
        AutoID: {
            itemName: “Apocalypse”,
            itemExpires: “December 21, 2012”,
            itemType: “Catastrophic”
        }
    }
}

Then I would query like this:

ref.child(userID!).child("MyStuff").observeSingleEvent(of: .value, with: { (snapshot) in
    for child in snapshot.children {    
        let child = child as? DataSnapshot
        let key = child?.key as? String

        if let todoElement = child?.value as? [String: Any] {
            let todo = TheItems()
            todo.itemName = todoElement["itemName"] as? String
            todo.itemExpires = todoElement["itemExpires"] as? String
            todo.itemType = todoElement["itemType"] as? String

            self.itemList.append(todo)
            self.tableView.reloadData()
        }
    }
})

Additionally, like I said in my comment you can just upload the key with the data if you’re using .updateChildValues(). Example:

let key = ref.child("userID!").childByAutoId().key
let feed = ["key": key,
         “itemName”: itemName] as [String: Any]

let post = ["\(key)" : feed]

ref.child("userID").child("MyStuff").updateChildValues(post) // might want a completionBlock

Then you can get the key the same way you are getting the rest of the values. So your new data would look like this:

Uid: {
    MyStuff: {
        AutoID: {
            itemName: “Apocalypse”,
            itemExpires: “December 21, 2012”,
            itemType: “Catastrophic”,
            key: “autoID”
        }
    }
}
DoesData
  • 6,594
  • 3
  • 39
  • 62
  • Thank you! I cannot express my appreciation enough! Just a quick question to add on.. When it comes to editing an item, would you suggest a separate ViewController to handle the editing or somehow rig that VC that adds an item to also edit it? Thanks again! You're awesome! – Bradley Carter Sep 06 '17 at 06:41
  • Adding another viewController to edit depends on the UI you are going for. Does it make sense to change screens to edit data? If the data is a simple list in a tableView it might make more sense to edit it right there on the same screen. If it's more elaborate then a separate screen to edit information makes more sense. – DoesData Sep 06 '17 at 12:58