firebase Detect errors when reading data on iOS


Example

A frequent reason why your read operation may not work 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 Console output. 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 read on iOS you must implement the withCancel block of your observer:

    ref!.child("notAllowed").observe(.value, with: { (snapshot) in
        print("Got non-existing value: \(snapshot.key)")
    }, withCancel: { (error) in
        print(error)
    })