簡體   English   中英

setValue()的完成會導致Firebase,Swift 3崩潰

[英]Completion of setValue() causes crash Firebase, Swift 3

我當前正在將一個應用程序更新為Swift 3和iOS10。問題出在我何時使用:

    self.ref.setValue(value, withCompletionBlock: { (error: Error?, _:FIRDatabaseReference) in
            //Code
        })

該應用程序崩潰,沒有任何有關其執行該操作的信息。 如果刪除完成,則可以正常工作。

試試這個代碼,我希望這可以解決問題

// U can use this to set value to your database
func setValue() {
    let myRef = FIRDatabase.database().reference().child("Your path")
    let valueForChild: String = "newValue"
    let newValue = ["childName": valueForChild] as [String: Any]
    myRef.setValue(newValue) { (error, ref) in
        if error != nil {
            print(error?.localizedDescription ?? "Failed to update value")
        } else {
            print("Success update newValue to database")
        }
    }
}

// or this to update new value to your database
func updateValue() {
    let myRef = FIRDatabase.database().reference().child("Your path")
    let valueForChild: String = "newValue"
    let newValue = ["childName": valueForChild] as [String: Any]
    myRef.updateChildValues(newValue) { (error, ref) in
        if error != nil {
            print(error?.localizedDescription, "Failed to update value")
        } else {
            print("Success update newValue to database")
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM