简体   繁体   中英

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? 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. 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. 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

"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?

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. Setting the refs to nil at the end of the action solved my issue and I started seeing the multiple entries in the dashboard.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM