Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase checking for null value (Swift)

I'm running the code below to see if a user that opens the app is already logged in, and then to check if they've set up a profile. I'm having trouble checking for the null value value returned from the profile check

override func viewDidLoad() {
    super.viewDidLoad()

    //Check to see if user is already logged in
    //by checking Firebase to see if authData is not nil
    if ref.authData != nil {

        //If user is already logged in, get their uid.
        //Then check Firebase to see if the uid already has a profile set up
        let uid = ref.authData.uid
        ref.queryOrderedByChild("uid").queryEqualToValue(uid).observeSingleEventOfType(.Value, withBlock: { snapshot in
                let profile = snapshot.value
                print(profile)
        })

In the last line when I print(profile), I either get the profile information, or

 <null>

how do i check for this value?

 if profile == nil 

does not work

If I do

let profile = snapshot.value as? String

first, then it always returns nil even if there is a snapshot.value

like image 226
FortuneFaded Avatar asked Nov 28 '22 05:11

FortuneFaded


2 Answers

Take advantage of the exists() method to determine if the snapshot contains a value. To use your example:

let uid = ref.authData.uid
ref.queryOrderedByChild("uid").queryEqualToValue(uid)
         .observeSingleEventOfType(.Value, withBlock: { snapshot in

    guard snapshot.exists() else{
        print("User doesn't exist")
        return
    }

    print("User \(snapshot.value) exists")
})

.

Here's another handy example, Swift4

    let path = "userInfo/" + id + "/followers/" + rowId

    let ref = Database.database().reference(withPath: path)

    ref.observe(.value) { (snapshot) in

            let following: Bool = snapshot.exists()

            icon = yesWeAreFollowing ? "tick" : "cross"
        }
like image 192
Erken Avatar answered Dec 09 '22 13:12

Erken


You may want to explore another option: since you know the uid of the user and therefore the path to that user, there's no reason to query. Queries add unnecessary overhead in situations where you know the path.

For example

users
  uid_0
    name: "some name"
    address: "some address"

You are much better off observing the node in question by value, which will return null if it doesn't exist

ref = "your-app/users/uid_0"

ref.observeEventType(.Value, withBlock: { snapshot in
    if snapshot.value is NSNull {
        print("This path was null!")
    } else {
        print("This path exists")
    }
})

in the event you are storing it some other way; perhaps

random_node_id
   uid: their_uid
   name: "some name"

then a query would be in order, like this

ref.queryOrderedByChild("uid").queryEqualToValue(their_uid)
   .observeEventType(.Value, withBlock: { snapshot in

       if snapshot.exists() {
           print("you found it!")
       }

   });
like image 24
Jay Avatar answered Dec 09 '22 12:12

Jay