firebase How do I listen for errors when accessing the database? Detect errors when writing a value on iOS


There are many reasons a write operation may fail. A frequent one is because your security rules reject the operation, for example because you're not authenticated (by default a database can only be accessed by an authenticated user).

You can see these security rule violations in the output of your program. But it's easy to overlook these. You can also handle them in your own code and make them more prominently visible, which is especially useful during development (since your JSON, rules and code change often).

To detect a failed write on iOS you attach a completion block to setValue:

let message = ["name": "puf", "text": "Hello from iOS"]
ref!.childByAutoId().setValue(message) { (error) in
    print("Error while writing message \(error)")

Throwing an exception like this ensures that it will be very difficult to overlook such an error next time.