I was trying to update my Firebase database and I ran into this problem. Take a look at the following code snippet and the screenshot:
func saveRetrieveStoryID(completion: @escaping (Bool) -> Void) {
let userID = Auth.auth().currentUser?.uid
//Create a reference to the database
let DBRef = Database.database().reference()
let storyIDRef = DBRef.child("Story IDs").child(userID!)
storyIDRef.observe(.value) { (snapshot) in
for childOne in snapshot.children {
print(childOne)
if let childOneSnapshot = childOne as? DataSnapshot {
storyIDKeyList.append(Int(childOneSnapshot.key)!)
print(childOneSnapshot.key)
completion(true)
}
}
print(storyIDKeyList)
}
}
What the code does is that it retrieves the key (-1) from the database and stores it inside a list (storyIDKeyList). Now take a look at the following code snippet:
saveRetrieveStoryID { (saved) in
if saved {
// Store the story ID in the user's story ID dict
let storyIDRef = DBRef.child("Story IDs").child(userID!)
let newStoryIDKey = storyIDKeyList.last! + 1
storyIDs[String(newStoryIDKey)] = storyRef.key
storyIDRef.updateChildValues(storyIDs, withCompletionBlock: { (error, ref) in
if let error = error?.localizedDescription {
print("Failed to update databse with error: ", error)
}
})
}
}
This piece of code, takes the last item from the storyIDKeyList and adds 1 to it. Then this will be added to the storyIDs dictionary storyIDs[String(newStoryIDKey)] = storyRef.key
and the database will be update with the new key and value. But the problem is that, the database keeps on updating and it doesn't stop until I stop running the code. Here is a picture of the resulting database:
Notice that all the values are the same. This following screenshot should be the expected outcome:
I just want to add one key/value to the database each time I run the code; I kind of know why this is happening but I'm finding it difficult to solve this problem.
After a lot of tries, I managed to find a solution to this problem.
Edit: I found a better solution, thanks to this answer: Android Firebase Database keeps updating value. Using observeSingleEvent() retrieves the data only once.
Here is the code (Better answer IMO):
func saveRetrieveStoryID(completion: @escaping (Bool) -> Void) {
let userID = Auth.auth().currentUser?.uid
let storyIDRef = DBRef.child("Story IDs").child(userID!)
storyIDRef.observeSingleEvent(of: .value) { (snapshot) in
for childOne in snapshot.children {
if let childOneSnapshot = childOne as? DataSnapshot {
storyIDKeyList.append(Int(childOneSnapshot.key)!)
}
}
completion(true)
}
}
Old answer (Works too):
func saveRetrieveStoryID(completion: @escaping (Bool) -> Void) {
let userID = Auth.auth().currentUser?.uid
let storyIDRef = DBRef.child("Story IDs").child(userID!)
storyIDRef.observe(.value) { (snapshot) in
for childOne in snapshot.children {
if let childOneSnapshot = childOne as? DataSnapshot {
storyIDKeyList.append(Int(childOneSnapshot.key)!)
}
}
storyIDRef.removeAllObservers()
completion(true)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With