astrocat1997
astrocat1997

Reputation: 187

firebase & swift - code in ref.observeSingleEventOfType not being run

I am making a sign in/out app that has a SIGN IN/SIGN OUT button that changes to whichever the person will do next. For instance, if they are signed in, it will say SIGN OUT. The information about whether they are signed in or not is all gotten from Firebase.

Anyways, I'm trying to give the bool currently_in a value in the ...ref.observeSingleEventOfType(.Value... code block but the program never gets in there. I put a breakpoint even at print(currently_in) and it never stopped the program.

I'm very new to Firebase and Swift and even StackOverflow so anything that you can offer would be very helpful. Thank you!

func refresh_sign_in_out_button() {
    var button_title = "SIGN IN/OUT"

    if let op_user = user {
        var currently_in: Bool?

        op_user.user_ref!.observeSingleEventOfType(.Value, withBlock: { snapshot in
            currently_in = (snapshot.childSnapshotForPath("current_status").value as! String == "IN")
            print(currently_in)
        })

        if !currently_in! {
            button_title = "SIGN IN"
            type_of_log_in.hidden = false
            sign_in_out_button.setTitleColor(UIColor(red: 0.349, green: 0.89, blue: 0.0, alpha: 1.0), forState: UIControlState.Normal)
        } else {
            button_title = "SIGN OUT"
            op_user.user_log_in_type = type_of_log_in_label.text
            type_of_log_in.hidden = true
            sign_in_out_button.setTitleColor(UIColor(red: 0.922, green: 0.0, blue: 0.231, alpha: 1.0), forState: UIControlState.Normal)
        }
    } else {
        button_title = "SIGN IN"
        type_of_log_in.hidden = false
        sign_in_out_button.setTitleColor(UIColor(red: 0.349, green: 0.89, blue: 0.0, alpha: 1.0), forState: UIControlState.Normal)
    }

My project is here: Sign In/Out App

Firebase structure:

{
  "mentors" : {
    "Ash Dreyer" : {
      "current_status" : "IN",
      "logins" : [ null, {
        "sign_in_time" : "2016-05-15 18:44:59 +0000"
      } ],
      "num_of_logins" : 0,
      "total_hours" : 0
    },
    "Donald Pinckney" : {
      "current_status" : "OUT",
      "num_of_logins" : 0,
      "total_hours" : 0
    },
    "Jasmine Zhou" : {
      "current_status" : "OUT",
      "num_of_logins" : 0,
      "total_hours" : 0
    },
    "Michael Corsetto" : {
      "current_status" : "OUT",
      "num_of_logins" : 0,
      "total_hours" : 0
    }
  },
  "students" : {
    "Bryton Moeller" : {
      "current_status" : "OUT",
      "num_of_logins" : 0,
      "total_hours" : 0
    },
    "Kelly Ostrom" : {
      "current_status" : "OUT",
      "num_of_logins" : 0,
      "total_hours" : 0
    },
    "Kyle Stachowicz" : {
      "current_status" : "OUT",
      "num_of_logins" : 0,
      "total_hours" : 0
    },
    "Wesley Aptekar-Cassels" : {
      "current_status" : "IN",
      "logins" : [ null, {
        "sign_in_time" : "2016-05-15 16:14:11 +0000"
      } ],
      "num_of_logins" : 0,
      "total_hours" : 0
    }
  }
}

I don't have user IDs for the students and mentors because the data is only needed for one year and then we replace it with new data.

Upvotes: 1

Views: 578

Answers (1)

astrocat1997
astrocat1997

Reputation: 187

So I talked about this with a friend and he solved my problem. Here's my code from that:

func refresh_sign_in_out_button(currently_in: Bool) {
    var button_title = "SIGN IN/OUT"

    if let op_user = user {
        if !currently_in {
            button_title = "SIGN IN"
            type_of_log_in.hidden = false
            sign_in_out_button.setTitleColor(UIColor(red: 0.349, green: 0.89, blue: 0.0, alpha: 1.0), forState: UIControlState.Normal)
        } else {
            button_title = "SIGN OUT"
            op_user.user_log_in_type = type_of_log_in_label.text
            type_of_log_in.hidden = true
            sign_in_out_button.setTitleColor(UIColor(red: 0.922, green: 0.0, blue: 0.231, alpha: 1.0), forState: UIControlState.Normal)
        }
    } else {
        button_title = "SIGN IN"
        type_of_log_in.hidden = false
        sign_in_out_button.setTitleColor(UIColor(red: 0.349, green: 0.89, blue: 0.0, alpha: 1.0), forState: UIControlState.Normal)
    }

    sign_in_out_button.setTitle(button_title, forState: .Normal)
}

func refresh_sign_in_out_button() {
    if let op_user = user {
        op_user.user_ref!.observeEventType(.Value, withBlock: { snapshot in
            let currently_in = (snapshot.childSnapshotForPath("current_status").value as! String == "IN")
            print(currently_in)
            self.refresh_sign_in_out_button(currently_in)
            }, withCancelBlock: { error in
                print(error.description)
        })
    } else {
        refresh_sign_in_out_button(false)
    }
}

We made a separate function that gets the value of current_status and feeds it into the original function. I don't really understand why this works but it can't hurt to post what I did here.

Upvotes: 1

Related Questions