Apneist
Apneist

Reputation: 406

Firebase erroneously shows deleted data / Swift 3 / Xcode 8.2

I am new to coding and just started using Firebase, and I am running the below function which is basically trying to create 2 nodes, "Tournaments" and "Daily Games" (if they don't exist already).

The database tree should look like this:

-London
  -City Game
      -Date(stringDate)  // getting this from another function, returns 2016-16-12
           -Tournaments
           -Daily Games

And the code looks like this:

     func createTournamentsAndNonTournamentsNodes() {

        let databaseRef = FIRDatabase.database().reference()
        self.getLiveTimestamp()  // this calls a function that returns timestamp


        databaseRef.child("London/City Game/\(stringDate)").observeSingleEvent(of: .value, with:  { (snapshot) in

            if snapshot.hasChild("Daily Games") {
                print ("Daily Games node exists already")
            print(snapshot.key)
            print(snapshot.value as Any)
            } else {
                print ("Daily Games dont exist, creating it now")
                let postTwo : [String : AnyObject] = [("Daily Games") : "Initiated today at : \(self.timeStampString)" as AnyObject]

                databaseRef.child("London/City Game/\(stringDate)").child("Daily Games").updateChildValues(postTwo)
            }
            if snapshot.hasChild("Tournaments") {
                print ("Tournaments node exists already")
            print(snapshot.key)
            print(snapshot.value as Any)
            } else {
                print ("Tournaments dont exist, creating it now")
                let postOne : [String : AnyObject] = [("Tournaments") : "Initiated today at : \(self.timeStampString)" as AnyObject]
                databaseRef.child("London/City Game/\(stringDate)").child("Tournaments").updateChildValues(postOne)
            }
        })
    }

So the result of running the above on an empty database is that the console prints "Daily game node exists already" and "Tournaments node exists already", with key and values as below. But the problem is that those were created by me on a previous run 1 hr ago and immediately deleted manually from the Firebase web console, to test the code, making my database completely empty before running the code again. So reloading the Firebase console shows me an empty database, but my code prints me this, as if the data exists in the database. How is this possible? Does Firebase keep data for some time despite appearing empty on the web console?

Optional({

    "Daily Games" = "Initiated today at : 13:01:19";
    Tournaments =     {
        Tournaments = "Initiated today at : 13:05:22";
    };
})

What am I doing wrong? How to really tell what is and what is not in the database if my code shows me a populated database, but my Web Console shows me an empty one? Many thanks

UPDATE:

I had FIRDatabase.database().persistenceEnabled = true and also running this on a real device. After disabling persistenceEnabled, it works absolutely fine. What is the way to fix this, without disabling persistenceEnabled?

Upvotes: 2

Views: 527

Answers (1)

Alisson Enz
Alisson Enz

Reputation: 143

Well, here is my tip.

I had almost the same problem, and to solve this I changed from observeSingleEvent to observe and then Firebase does not only look into your cellphone, because that's exactly what it does when you do it.

Upvotes: 4

Related Questions