没有使用Firebase运行observerSingleEvent函数

时间:2017-05-02 10:36:50

标签: swift firebase firebase-realtime-database

我正在尝试运行一个函数,以便能够使用Firebase从实时数据库中检索数据,但每当我运行该函数时;我的函数的observerSingleEvent部分将无法运行,我已经尝试将一个print语句放入其中并且它没有被运行,也没有将字段读入变量,任何帮助都是有益的。

func checkIfNewDay() -> Bool {
    print(self.currDate)
    var ref: FIRDatabaseReference!
    ref = FIRDatabase.database().reference()
    let userID = FIRAuth.auth()?.currentUser?.uid
    print("outside function")
    ref.child("user").child(userID!).child("dates").observeSingleEvent(of: .value, with: { (snapshot) in
        // Get user value
        print("inside function")
        let value = snapshot.value as? NSDictionary
        print("just to make sure its going inside the function.  Delete after")
        self.lastDate = value?["lastSaveDate"] as? String ?? "Date Invalid"
        self.newLastDate = String(self.lastDate)
        if self.newLastDate != "Date Invalid" {
            print(self.lastDate)
        } else {
            print("Error, date not able to be recieved from the database")
            self.catchGetDateError = true
            self.saveCurrentDate()
        }
    })



    if (!self.catchGetDateError) {
        print(self.newLastDate, "newLastDate")
        print(self.currentDate, "currentDate")

        if (self.newLastDate == self.currentDate) {
            print("Day has not moved on.")
            return false
        } else {
            print("Day has moved on!")
            return true
        }
    }
    return true
}

我为这个很长的功能道歉 - 这是一个非常奇怪的写作。

1 个答案:

答案 0 :(得分:1)

根据评论,我想我已经明白了,你想要什么。

为了获得像sync这样的结果,您需要实现转义。像这样:

func checkIfNewDay(completion: @escaping (_ isNew: Bool) -> Void) {
    print(self.currDate)
    var ref: FIRDatabaseReference!
    ref = FIRDatabase.database().reference()
    let userID = FIRAuth.auth()?.currentUser?.uid
    print("outside function")
    ref.child("user").child(userID!).child("dates").observeSingleEvent(of: .value, with: { (snapshot) in
        // Get user value
        print("inside function")
        let value = snapshot.value as? NSDictionary
        print("just to make sure its going inside the function.  Delete after")
        self.lastDate = value?["lastSaveDate"] as? String ?? "Date Invalid"
        self.newLastDate = String(self.lastDate)
        if self.newLastDate != "Date Invalid" {
            print(self.lastDate)
            if (self.newLastDate == self.currentDate) {
                print("Day has not moved on.")
                completion(false)
            } else {
                print("Day has moved on!")
                completion(true)
            }
        } else {
            print("Error, date not able to be recieved from the database")
            self.catchGetDateError = true
            self.saveCurrentDate()
            completion(false)
        }
    })
}

所以,现在你可以使用你的func:

checkIfNewDay(completion: { isNew in
     // do whatever you want. isNew will have info, that you need.
})

你应该拥有它,因为.observe函数工作异步。你应该理解这个想法。

希望它有所帮助。