samedi 18 avril 2020

Why does my conditional statement work sometimes and fail the others when I retrieve a value from Cloud Firestore?

I have been trying to retrieve a boolean from Firestore to be able to control the state of my app. For instance, if the retrieved value is true (ie 1), the view should act one way and if its false (ie 0) it should act another way.

However, this fails sometimes and works the others, which is why I haven't been able to solve this issue myself! Below is my code, that I have called in viewWillAppear() (I'm hoping that is correct, since I do want it to be decided every time my view appears.

func getStatus() {
    let db = Firestore.firestore()
    let docRef = db.collection("collection").document("doc")

    docRef.addSnapshotListener { (doc, e55) in
        guard let doc = doc, doc.exists else {
            return
        }

        guard let instruction = doc.get("new") else {
            return
        }

        if instruction as? Bool == true {
            UIView.animate(withDuration: 3, animations: {
                self.button.alpha = 0
            })
        } else if instruction as? Bool == false {
            self.view.backgroundColor = .black
        }


    }
}

This function gets called in viewWillAppear(). However, sometimes, both happen, ie, the button disappears as well as the backGround turns black.

[Please do let me know if this question is not allowed, or if I need to make further clarifications :)]

Aucun commentaire:

Enregistrer un commentaire