Ios 使用updateChildValues更新childByAutoId

Ios 使用updateChildValues更新childByAutoId,ios,xcode,firebase,firebase-realtime-database,Ios,Xcode,Firebase,Firebase Realtime Database,我正在尝试使用childByAutoID和UpdateChildDat()更新firebase数据库中的子节点。当我执行代码时,类型被更新,但是它在节点之外,如下所示 类型:3应该在表示类型:2的节点内部更新 我用来执行此操作的代码如下所示 let DB_REF = Database.database().reference() let WORK_PROG_REF = DB_REF.child("workInProgress") func completeJobProgress()

我正在尝试使用childByAutoID和UpdateChildDat()更新firebase数据库中的子节点。当我执行代码时,类型被更新,但是它在节点之外,如下所示

类型:3应该在表示类型:2的节点内部更新

我用来执行此操作的代码如下所示

let DB_REF = Database.database().reference()
let WORK_PROG_REF = DB_REF.child("workInProgress")


    func completeJobProgress() {
    let workUser = self.workerUser
    let uid = Auth.auth().currentUser?.uid
    let workerId = workUser?.uid
    let address = workerUser?.address
    let createDate = Int(NSDate().timeIntervalSince1970)

    Database.database().reference().child("users").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
        guard let dictionary = snapshot.value as? [String : Any] else { return }
        let user = User(dictionary: dictionary as [String : AnyObject])
        workUser?.uid = snapshot.key

        let docData: [String: Any] = ["workerId": uid!,
                                      "creationDate": createDate,
                                      "fromId" : workerId!,
                                      "location": address!,
                                      "type": 3,
                                      "checked": 0,]


        self.setJobToCompletedInDatabase(uid: uid!, values: docData as [String : AnyObject])
        self.postJobNotificationsIntoDatabseWithUID(uid: workerId!, values: docData as [String : AnyObject])


//how I initially got the type: 3 to save under the node was by using the line below 

WORK_PROG_REF.child(uid!).child("type").setValue(3)


        print(workerId!)

     }, withCancel: { (err) in
        print("attempting to load information")
        })

        print("Finished saving user info")
        self.dismiss(animated: true, completion: {
            print("Dismissal complete")
        })


    }
这只是将类型设置为在节点下。我尝试使用下面的代码,但下面的一行显示错误,无法将“String”类型的值转换为预期的参数类型“[AnyHashable:Any]”

     WORK_PROG_REF.child(uid!).updateChildValues("type").setValue(3)
感谢所有帮助

尝试:

具有事务的解决方案:

WORK_PROG_REF.child(uid!).child("-M1bOYAcP_IMFbgz8siD").runTransactionBlock({ (result: MutableData) -> TransactionResult in
            if var objInfo = result.value as? [String: Any] {
                objInfo["type"] = 3

                // Set value and report transaction success
                result.value = objInfo

                return TransactionResult.success(withValue: result)
            }else{
                return TransactionResult.success(withValue: result)
            }
        }) { (error,completion,snap) in
            //Handle Error
        }
WORK_PROG_REF.child(uid!).child("-M1bOYAcP_IMFbgz8siD"). child("type").setValue(3)
无交易的解决方案:

WORK_PROG_REF.child(uid!).child("-M1bOYAcP_IMFbgz8siD").runTransactionBlock({ (result: MutableData) -> TransactionResult in
            if var objInfo = result.value as? [String: Any] {
                objInfo["type"] = 3

                // Set value and report transaction success
                result.value = objInfo

                return TransactionResult.success(withValue: result)
            }else{
                return TransactionResult.success(withValue: result)
            }
        }) { (error,completion,snap) in
            //Handle Error
        }
WORK_PROG_REF.child(uid!).child("-M1bOYAcP_IMFbgz8siD"). child("type").setValue(3)

我希望这能解决您的问题。

我得到的错误值类型为“DatabaseQuery”没有成员“runTransactionBlock”。我已更新了我的答案,您的代码中是否有id:-M1bOYAcP\U IMFbgz8siD?如果是,上述解决方案将起作用。抱歉,响应延迟,但它不起作用。我的代码中没有-M1bOYAcP_IMFbgz8siD id当您获取该数据时,您将该值作为密钥获取,现在您必须只使用值。您的代码最终运行良好。但是为什么需要-M1bOYAcP_IMFbgz8siD来运行这个呢?如果有不同的autoId,这是否仍然有效?