ios - 更新值 Firebase Swift 3

标签 ios swift firebase swift3 firebase-realtime-database

我在使用键 status 更新我的 Firebase 数据库中的值时遇到问题。我只是不知道如何访问它是 autoID 的 child(id)。我的 Swift 3 实体中应该有一个属性吗?还是有 child (autoID)或类似的东西?我是 Firebase 和 Swift 3 的新手。任何人都可以帮忙吗?这是我的代码:

 self.ref?.child("requests").queryOrdered(byChild: "Sender").queryEqual(toValue: self.items[indexPath.row].Sender).setValue("2", forKey: "status")) 

click here to see an image showing my database

最佳答案

swift 3 && 火力地堡 3

希望这段代码能帮到你...

    // create the reference you want to observe
    let myRequestRef = FIRDatabase.database().reference().child("requests").queryOrdered(byChild: "Sender").queryEqual(toValue: self.items[indexPath.row].Sender)

    // check the sender value
    myRequestRef.observeSingleEvent(of: .value, with: { (snapshot) in

        //get the sender key
        let senderKey = snapshot.key as String

        // create status ref for new status value
        let statusRef = FIRDatabase.database().reference().child("requests").child(senderKey)

        // create new dict for status value
        let newValue = ["Status": 2] as [String: Any]

        statusRef.updateChildValues(newValue, withCompletionBlock: { (error, _) in
            if error != nil {
                print(error?.localizedDescription ?? "Failed to set status value")
            }
            print("Successfully set status value")
            // Update your UI
            DispatchQueue.main.async {
                // Do anything with your UI
            }
        })
    }) { (error) in
        print("Failed to get snapshot", error)
    }

关于ios - 更新值 Firebase Swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42798791/

相关文章:

ios - 使用 Swift 在 Twitter 上实现单用户 OAuth

swift - SQLITE 和 Swift 3 挑战

java - 从 Firebase 存储到 Recycler View 的图像

ios - 来自 Google 的意外 403 错误

ios - 检查 SKCameraNode View 是否包含某个类的节点?

node.js - 谷歌 API 网关 + Firebase : X-Apigateway-Api-Userinfo vs X-Forwarded-Authorization headers

java - 无法解析 firebase-ui-auth

ios - 嵌入到选项卡栏 Controller 偏移 TableView

ios - 如何在 iOS8 自定义键盘中像键盘字符一样弹出?

ios - 无法覆盖 NSFetchedResultsControllerDelegate 中的controllerDidChangeContent