简体   繁体   中英

Swift 3 Firebase retrieving key and passing to view controller

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)
    }


}

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

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”
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM