简体   繁体   English

iOS的Firebase:childByAutoId()每次都会覆盖同一个孩子

[英]Firebase for iOS: childByAutoId() overrides the same child every time

I am working with Firebase for the second time now, but for the first with Firebase for iOS.(and with iOS in general) So I am using childByAutoId() which should be like push() for Android, right? 我现在第二次使用Firebase,但第一次使用iOS的Firebase。(以及与iOS一起使用)所以我正在使用childByAutoId(),它应该类似于Android的push(),对吗? My code is: 我的代码是:

let quoteString = ["base64": base64String]
let refPhotos = refPlace.childByAppendingPath("Images")
let refBase54 = refPhotos.childByAutoId()
refBase54.setValue(quoteString)

But every time photo is saved in Firebase the previously saved photo (it is only one for now) gets overridden by the new one. 但是,每次将照片保存在Firebase中时,以前保存的照片(目前仅一张)都将被新的照片覆盖。 The ID is changing and I can see that for a second Firebase is creating new child "Images" and the two photos are separately saved in two different Images children of Place. ID正在更改,我可以看到第二个Firebase正在创建新的子级“图像”,并且这两张照片分别保存在Place的两个不同“图像”子级中。 And then it is gone. 然后它消失了。

So am I missing something obvious or is this a bug or any other advise is very welcome. 因此,我是否遗漏了一些明显的东西,或者这是一个错误或任何其他建议,我们非常欢迎。

Update: 更新:
my json data 我的json数据

"Places" : {
    "ChIJMaArcCHP5zsRKU_FpbNldp8" : {
      "Comments" : {
        "-K9xX2KBCiOZMthtbxSy" : "Foo"
     },
     "ID" : "ChIJMaArcCHP5zsRKU_FpbNldp8",
     "Images" : {
        "-K9xX2KUPSLGvFZpI-XL" : {
          "base64" : "very long"
          }

... and so on. ... 等等。 After exploring it a little - what can be seen in the moment of writing is both "Images" and "Comments" appear twice in red and green color(new is created, old is deleted). 经过一番探索后,在撰写本文时可以看到“图像”和“评论”都以红色和绿色两次出现(创建了新的,删除了旧的)。 This is not the goal of childByAppendingPath(), right? 这不是childByAppendingPath()的目标,对吗?

I just ran this snippet a few times: 我只运行了几段:

var rootRef = Firebase(url: "https://stackoverflow.firebaseio.com/35258493")

override func viewDidLoad() {
    super.viewDidLoad()

    let quoteString = ["base64": "SAIUH IE WE E EUWG EWG YGEFWU GEWOGEW "]
    let refPhotos = rootRef.childByAppendingPath("Images")
    let refBase54 = refPhotos.childByAutoId()
    refBase54.setValue(quoteString)
}

And ended up with this data: 并最终得到以下数据:

{
  "Images": {
    "-K9y4C3NhndlH9LwR7B4": {
      "base64": "SAIUH IE WE E EUWG EWG YGEFWU GEWOGEW "
    },
    "-K9y4I3otW6hV59C6Z2G": {
      "base64": "SAIUH IE WE E EUWG EWG YGEFWU GEWOGEW "
    }
  }
}

So the second "image" was clearly not replacing the first one. 因此,第二个“图像”显然不能替代第一个。

Not sure if you worked this out. 不知道您是否解决了这个问题。 But I was also facing the same issue. 但是我也面临着同样的问题。 My code was inside a UIButton action and all my new entries kept getting deleted and only the latest one was retained. 我的代码在UIButton动作中,所有新条目均被删除,仅保留了最新条目。 Setting the refs to nil at the end of the action solved my issue and I started seeing the multiple entries in the dashboard. 在操作结束时将refs设置为nil可以解决我的问题,并且我开始在仪表板中看到多个条目。

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

相关问题 iOS Firebase-如何使用.childByAutoId()创建多节点位置更新 - iOS Firebase -How to create multi node location update using .childByAutoId() Firebase Swift childByAutoId() - Firebase Swift childByAutoId() Firebase数据库读取childByAutoId - Firebase Database Read childByAutoId 在 Firebase iOS SDK 中,每次删除子项时都会触发 .childAdded。 我怎么能阻止这个? - In Firebase iOS SDK, .childAdded is triggered every time a child is deleted. How can I stop this? Firebase,显示带有下一个路径的帖子“ self.dataBaseRef.child(“帖子”).child(uid).childByAutoId()” - Firebase ,Display post with The next path “self.dataBaseRef.child(”Posts“).child(uid).childByAutoId()” Firebase - 当您不知道父键的值时访问子节​​点 - 由childByAutoId()生成 - Firebase - accessing child node when you don't know the value of the parent key - generated by childByAutoId() 如何使用childByAutoId更新Firebase中的值? - How to update value in Firebase with childByAutoId? firebase childByAutoId.key是可选的 - firebase childByAutoId.key is optional 每次将子级添加到firebase数据库时,都会调用viewDidLoad - viewDidLoad getting called every time a child is added to firebase database 访问childByAutoId Firebase Swift中的特定数据 - Access specific data in childByAutoId Firebase Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM