firebase观察方法 - 零星的停机时间



我使用观察力的ios中的观测值随机下降与firebase的连接,并观察iOS中的方法。

我使用多种方法使用其REST API连接到Firebase数据库。

我有时会使用整个URL的方法,例如https://example.firebaseio.com/problems.json。这总是正确返回数据。

例如,当我使用时(观察到的levent(时,有时在这种情况下根本没有代码!在视图控制器之间,这是一致的。

数据库仍然可以使用URL方法100%起作用,但是有时使用观察levent它确实有效!但是没有更改任何代码,有时这些事件只是停止工作。我尝试重建,登录和登录,两者的组合,甚至得出结论,即如果我离开它,它会再次起作用。

有人有什么合乎逻辑的原因,为什么发生零星的下降时发生,以及我可以做些什么来解决它?当代码工作时,我不更改它,但是随后在整个应用程序中停止了一段时间。

感谢您的帮助。以下是有时会运行的代码的示例,有时不会运行。

func getComments() -> Int{
    print("getting comments")
    let ref = FIRDatabase.database().reference(withPath: "comments")
    let query = ref.queryOrdered(byChild: "problem_id").queryEqual(toValue: self.id)
    print("Starting observing");
    query.observeSingleEvent(of: .value, with: { (snapshot) in
        print("Got snapshot");
        print(snapshot.childrenCount)
        self.commentCount = Int(snapshot.childrenCount)
    })
    print("returning the comment count");
    return commentCount
}

我对firebase是新手在代码下一步进行之前执行。我建议尝试(我不肯定它将起作用(是修改您的代码,以便需要完成一个完整的处理程序,以确保观察块中的代码在获得结果之前已完成。

,您的新代码将是

func getComments(completion: @escaping (Int) -> Void) {
    print("getting comments")
    let ref = FIRDatabase.database().reference(withPath: "comments")
    let query = ref.queryOrdered(byChild: "problem_id").queryEqual(toValue: self.id)
    print("Starting observing");
    query.observeSingleEvent(of: .value, with: { (snapshot) in
        print("Got snapshot");
        print(snapshot.childrenCount)
        print("returning the comment count")
        let commentCount = Int(snapshot.childrenCount)
        self.commentCount = commentCount
        completion(commentCount)
    })
}

然后,当您要使用代码时,您将其称为

getComments(completion: { commentCount in
    print(commentCount)
    //Do other stuff with comment count
})

这很丑陋,但这是我第一次有类似问题时提出的解决方案。

最新更新