Firebase:从实时数据库中检索childByAutoID

我正在学习Firebase和iOS的绳索,所以请耐心等待。 我目前正在发布到名为Posts表中,如下所示:

  let postInfo = ["Description": txtPostDescription.text!, "ImageUrl": imgUrl, "Likes": 0] var t = FIRDatabase.database().reference().child("Posts").childByAutoId().setValue(postInfo) print(t) 

我正在尝试检索插入新记录时创建的primary key ,但是我无法检索它。 我已经尝试过childByAutoId()但由于浏览网页并且无法找到可靠的解决方案,这是随机猜测 )。 有任何想法吗?

 let postInfo = ["Description": txtPostDescription.text!, "ImageUrl": imgUrl, "Likes": 0] var reference = FIRDatabase.database().reference().child("Posts").childByAutoId() reference.setValue(postInfo) let childautoID = reference.key print(childautoID) 

注意 : – Althogh childByAutoId()是来自Firebase一个很棒的function。但是当你想用一个唯一的密钥创建一个节点时,更喜欢timestamps来存储数据。我更喜欢时间戳的原因是因为它们也有助于排序数据…但那就是我……

替代方案: –

 let timeStamp = Int(NSDate.timeIntervalSinceReferenceDate()*1000) //Will give you a unique id every second or even millisecond if you want.. FIRDatabase.database().reference().child("Posts").child(timeStamp).setValue(postInfo)