Home > other >  Cannot add dynamic data to Firebase RealtimeDatabase with Swift
Cannot add dynamic data to Firebase RealtimeDatabase with Swift

Time:07-31

I'm having this problem, but I cannot figure out why I can't add new items, or better, maybe I can add but the previous item is being substituted. Here is a video.

Here is the video

this is the code:

ZStack {
//...    
Button(action: {
                print(locali)
                Auth.auth().addStateDidChangeListener { auth, user in
                    print(auth.currentUser?.email)
                    ref
                        .child("locali")
                        .child(auth.currentUser?.uid ?? "no uid")
                        .setValue(["nomeLocale\(locali.count)" : nomeLocale])
                }
}, label: {
           //...
}
           //...
}
.onAppear {
          ref.child("locali").child(userID!).observe(.value, with: { snapshot in
                print(snapshot)
                guard let value = snapshot.value as? [String : NSObject] else {
                    print("error with the guard first")
                    return
                }
                print(value)
                guard let nomeInValue = value["nomeLocale\(locali.count)"] as? String else {
                    print("error with the guard second")
                    return
                }
                print(nomeInValue)
                locali.append(nomeInValue)
            })
        }
}

CodePudding user response:

When you call setValue on a path in the database, all the existing data at that path is replaced with whatever you are passing in.

If you only want to update whatever child properties/paths you are passing in, use updateChildValues instead.

  • Related