bianca hinova
bianca hinova

Reputation: 607

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?

Upvotes: 0

Views: 2308

Answers (2)

DS.
DS.

Reputation: 3004

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.

Upvotes: 0

Frank van Puffelen
Frank van Puffelen

Reputation: 599736

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.

Upvotes: 3

Related Questions