M. Kremer
M. Kremer

Reputation: 679

Completion of setValue() causes crash Firebase, Swift 3

I am currently updating an App to Swift 3 and iOS 10. The Problem is whenever I use:

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

The App crashes without any kind of information on why it does that. If I delete the completion, it works fine.

Upvotes: 1

Views: 1090

Answers (1)

Andrian Rahardja
Andrian Rahardja

Reputation: 344

Try this code, i hope this will do the trick

// 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")
        }
    }
}

Upvotes: 2

Related Questions