简体   繁体   English

Swift 3 Firebase检索密钥并传递给视图控制器

[英]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. 简单的应用程序从Firebase数据库检索数据,然后从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 . 主数据将通过,但是如果没有标识的“键”(我尝试将其设置为childByAutoID()但随后更改为timestamp childByAutoID()我将无法编辑信息。 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 您要查找的键位于for循环的迭代器中

Inside your if-let, try to do this: 在if-let内部,尝试执行以下操作:

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() . 另外,就像我在评论中说的那样,如果您使用的是.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”
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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