Firebase : Retrieve childByAutoID from Realtime Database

后端 未结 1 1630
你的背包
你的背包 2020-12-18 06:33

I\'m currently learning the ropes of Firebase and iOS so please bear with me. I\'m currently posting to my table called Posts as shown here:

 l         


        
1条回答
  •  南笙
    南笙 (楼主)
    2020-12-18 06:58

    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)
    

    Note :- Althogh childByAutoId() is a great functionality from Firebase.But prefer timestamps to store the data into when you want to create a node with a unique key.The reason why i prefer timestamps is because they can also be helpful in sorting of data...But thats just me..

    Alternative :-

    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)
    

    0 讨论(0)
提交回复
热议问题