我目前正在学习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)
注意:-AlthoghchildByAutoId()
是一个很棒的功能Firebase
。但是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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句