Firebase iOS-键/值对实际上并未从数据库中删除

时间:2018-07-23 21:49:46

标签: ios swift firebase firebase-realtime-database

我将所有用户名保存在一个单独的节点中,以便在用户搜索用户名时进行搜索。我从节点上删除了一个名字。 pizzaMan 。问题是即使它删除了,但如果我在我的应用程序中对已删除的名称进行搜索,它说它可用,但是当我物理查看数据库内部时,它显示它仍然存在(这意味着它不可用)。 那怎么可能?

@IBAction func deleteUsernameButtonTapped(_ sender: UIButton) {

    // the user's username is pizzaMan
    let username = usernamesRef?.child("pizzaMan")
    userName?.observeSingleEvent(of: .value, with: { (snapshot) in

        if snapshot.exists() {

            let key = snapshot.key

            username?.child(key).removeValue()
            print("username: \(key) has been deleted\n")
        }
    })
}

用户名pizzaMan已被删除,但实际上它在数据库中显示仍然存在。

enter image description here

enter image description here

let checkUsernameTextField = UITextField()
checkUsernameTextField.addTarget(self, action: #selector(handleSearchForUsername), for: .editingChanged) 

@objc func handleSearchForUsername() {

    // now search for pizzaMan inside a textField
    usernamesRef?.observeSingleEvent(of: .value, with: { (snapshot) in

        if snapshot.exists() {

            print("name is NOT avail")
        } else {
            print("name IS available") // this prints when searching for pizzaMan even though it's inside the db??
        }
    })
}

如果我尝试获取它,可以让我用新值覆盖旧值,但是一旦删除,它仍然不应显示在数据库中。

1 个答案:

答案 0 :(得分:0)

如果我了解您的问题,那么您的问题就在您的删除函数中。

让我重申我认为你在说什么。您要完全删除pizzaMan的节点,是吗?试试这个:

@IBAction func deleteUsernameButtonTapped(_ sender: UIButton) {

    // the user's username is pizzaMan
    if let username = usernamesRef as? DatabaseReference{
         username.child("usernames/pizzaMan").removeValue()
    }
    else{
         print("Errors")
    }

}

您打算做什么?