Firebase asks for non-calling block when no results

I am using Firebase and I want to ask if something exists. It is called when the value is found, but the block is not called when nothing is found. Is this expected behavior?

    ref.queryOrderedByKey().queryEqualToValue(channelName).observeSingleEventOfType(.ChildAdded, withBlock: { snapshot in
        print("found channel:  \(snapshot.key)")
        }, withCancelBlock: { error in
        print(error.description)
    })

Did I do something wrong? Thanks

+4
source share
1 answer

To check for missing data (snapshot == NULL), he did this

    let refToCheck = myRootRef.childByAppendingPath(channelNameString)
    refToCheck.observeEventType(.Value, withBlock: { snapshot in
        if snapshot.value is NSNull {
            print("snapshot was NULL")
        } else {
            print(snapshot)
        }
    })

The queries are pretty heavy compared to .observeEventType, and since you already know which specific path you will check, it will work much better.

Edit: you can also use

if snapshot.exists() { ... }

Firebase , , .

Firebase 3.x Swift 3.x

    let refToCheck = myRootRef.child(channelNameString)
    refToCheck.observe(.value, with: { snapshot in
        if snapshot.exists() {
            print("found the node")
        } else {
            print("node doesn't exist")
        }
    })

1) , .exists . 2) Firebase, , node , .

node , node, :

    let refToCheck = myRootRef.child(channelNameString)
    refToCheck.observeSingleEvent(of: .value, with: { snapshot in
        if snapshot.exists() {
            print("found the node")
        } else {
            print("node doesn't exist")
        }
    })
+7

Source: https://habr.com/ru/post/1622616/


All Articles