【问题标题】:update childByAutoId with updateChildValues使用 updateChildValues 更新 childByAutoId
【发布时间】:2020-03-05 04:23:52
【问题描述】:

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

type: 3 应该在表示 type: 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")
        })


    }

这只是将类型设置为节点下。我尝试使用下面的代码,但在下面的行中,我收到错误 Cannot convert value of type 'String' to expected argument type '[AnyHashable: Any]'

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

感谢大家的帮助

【问题讨论】:

    标签: ios xcode firebase firebase-realtime-database


    【解决方案1】:

    试试:

    事务解决方案:

    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_IMFbgz8siD?如果是,上述解决方案将起作用。
    • 抱歉,回复延迟,但没有奏效。我的代码中没有 -M1bOYAcP_IMFbgz8siD id
    • 当您获得该数据时,您将此值作为键,现在您必须只使用值。
    • 您的代码最终完美运行。但为什么需要 -M1bOYAcP_IMFbgz8siD 来运行它?如果有不同的 autoId,这仍然有效吗?
    猜你喜欢
    • 1970-01-01
    • 2017-02-04
    • 2016-11-22
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2016-06-14
    • 1970-01-01
    相关资源
    最近更新 更多