Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ObserveSingleEvent returns old data

I want to fetch the required app version number when the app starts. But I can't get the right key.

I have this code to fetch. I use observe single event because I use this method to check the required app version number. This method is only fired when the app starts to do the check.

func getVersion(completionHandler: @escaping (Result<Any?>) -> ()) {

     let ref: DatabaseReference! = Database.database().reference().child("version").child("IOS")

     ref?.observeSingleEvent(of: .value , with: { snapshot in

         if snapshot.exists() {    
            let recent = snapshot.value as!  NSDictionary  
            print(recent)
         }
     })
}

But it is returning old results? I have isPersistenceEnabled enabled at my Appdelegate.

This is the database structure:

enter image description here

I get no results when I use Database.database().reference().child("version").child("IOS"). snapshot.exists is false when I use that.

What I previously had was: - version | IOS - 1.0

And i get result when I use Database.database().reference().child("version"), namely {iOS => 1.0}. I don't get it because it was my old structure.

like image 785
da1lbi3 Avatar asked Oct 30 '22 04:10

da1lbi3


1 Answers

The Firebase Realtime Database synchronizes and stores a local copy of the data for active listeners. In addition, you can keep specific locations in sync.

let scoresRef = Database.database().reference(withPath: "scores")
scoresRef.keepSynced(true)

The Firebase Realtime Database client automatically downloads the data at these locations and keeps it in sync even if the reference has no active listeners. You can turn synchronization back off with the following line of code.

scoresRef.keepSynced(false)

Haven't really tried it but it should work.

like image 74
Muhammad Hassan Avatar answered Nov 15 '22 05:11

Muhammad Hassan