Firebase& swift - ref.observeSingleEventOfType中的代码未运行

时间:2016-05-15 20:32:39

标签: ios swift firebase firebase-realtime-database

我正在制作一个登录/退出应用程序,其中有一个SIGN IN / SIGN OUT按钮,该按钮会更改为下一个人将执行的任何操作。例如,如果他们已登录,则会显示SIGN OUT。有关他们是否已登录的信息全部来自Firebase。

无论如何,我试图给bool current_in一个值... ref.observeSingleEventOfType(.Value ...代码块但程序永远不会进入那里。我在打印时设置断点(current_in)它从未停止过该计划。

我对Firebase和Swift甚至是StackOverflow都很陌生,所以你能提供的任何东西都会非常有用。谢谢!

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)
    }

我的项目在这里:Sign In/Out App

Firebase结构:

{
  "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
    }
  }
}

我没有学生和导师的用户ID,因为数据只需要一年,然后我们用新数据替换它。

1 个答案:

答案 0 :(得分:1)

所以我和朋友谈了这件事,他解决了我的问题。这是我的代码:

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)
    }
}

我们创建了一个单独的函数,它获取current_status的值并将其提供给原始函数。我真的不明白为什么会这样,但发布我在这里所做的事情并不会有什么坏处。