Firebase database out of sync - Swift

Viewed 223

I have enabled persistence in my project, but now I am having a weird behavior. The app is now out of sync with my database, and needs to open and close, to update the information from my database.

I learned that I needed to use keepSync, to get the database in sync, but this is not working somehow.

Example:

I have this information in my database.

enter image description here

So to get the data I use this code:

ref?.child("Offers").observe(.childAdded, with: { (snapshot) in

            if let object = snapshot.value as? [String:AnyObject] {
                nearStores.append((object["Store"] as? String)!)
                nearSavings.append((object["Saving"] as? String)!)
                let checked = object["SetTime"] as! Int
                checkedDates.append(checked)
                let thisDate = object["Date"] as? Int
                let timeInterval = Double(thisDate!)
                let myNSDate = Date(timeIntervalSince1970: timeInterval)
                nearDates.append(myNSDate)
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: "updateannotations"), object: nil)
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: "refreshstores"), object: nil)
            }
        }) { (error) in
        self.createAlert(title: "Error in form", message: (error.localizedDescription))
        }

To get the data in sync, in my viewDidLoad I have this:

let offersPath = Database.database().reference(withPath: "Offers")
offersPath.keepSynced(true)

But for some reason the data is not up to date. The implemented code happens when the app loads, and should append the SetTime from the database. My app has a map, and the SetTime decieds witch annotations there are on the map. But when I change the data, it isn't showing the new data instant. The app needs to open and then restart, to update the data. How can that be?

0 Answers
Related