【问题标题】:Firebase : Retrieve childByAutoID from Realtime DatabaseFirebase:从实时数据库中检索 childByAutoID
【发布时间】:2016-09-05 10:10:47
【问题描述】:

我目前正在学习 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()但由于浏览网页而无法找到可靠的解决方案,这是一个随机猜测)。有什么想法吗?

【问题讨论】:

    标签: ios swift firebase firebase-realtime-database


    【解决方案1】:
    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)
    

    注意 :- Alhogh childByAutoId()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)
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-16
    • 2020-12-09
    • 1970-01-01
    • 2021-02-06
    • 1970-01-01
    • 2020-12-28
    • 1970-01-01
    • 2018-09-11
    相关资源
    最近更新 更多