swift - Firebase 数据库不会停止更新

标签 swift firebase firebase-realtime-database

我在尝试更新我的 Firebase 数据库时遇到了这个问题。看看下面的代码片段和屏幕截图:

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)
    }
}

Firebase database

代码的作用是从数据库中检索 key (-1) 并将其存储在列表 (storyIDKeyList) 中。现在看看下面的代码片段:

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)
            }
        })
    }
}

这段代码从 storyIDKeyList 中取出最后一项并向其添加 1。然后这将被添加到 storyIDs 字典 storyIDs[String(newStoryIDKey)] = storyRef.key 并且数据库将更新为新的键和值。但问题是,数据库不断更新,直到我停止运行代码才会停止。这是生成的数据库的图片:

Resulting database

请注意,所有值都是相同的。以下屏幕截图应该是预期的结果:

Expected outcome

我只想在每次运行代码时向数据库添加一个键/值;我有点知道为什么会这样,但我发现很难解决这个问题。

最佳答案

经过多次尝试,我设法找到了解决这个问题的方法。

编辑:我找到了更好的解决方案,多亏了这个答案:Android Firebase Database keeps updating value .使用 observeSingleEvent() 仅检索一次数据。

这是代码(海事组织的最佳答案):

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)
    }
}

旧答案(也有效):

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)
    }
}

关于swift - Firebase 数据库不会停止更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57713924/

相关文章:

ios - Swift - 在滚动时增大和缩小 UIView

swift - 如何从 Firebase 实时数据库检索数据?

swift - 将焦点放在 tabBar 下的项目上

android - Firebase 云消息传递 : Parsing data from the message even when the app is terminated

firebase - flutter/firebase 数据库中的一个简单查询

android - 仅在身份验证后运行 FirebaseInstanceIdService

ios - 离线时不会一直触发 Firebase 值事件

android - Firebase onChildAdded 第一次为空

ios - 如何在 iOS 中的 prepareForSegue 之前执行按钮按下操作?

ios - tableViewCell 中的约束未正确显示