3

I have a problem with my connection between my app and firebase database. After adding:

Database.database().isPersistenceEnabled = true

To my AppDelegate, some of the data is out of sync. To get my data i use:

self.ref?.child("Stores").observe(.childAdded, with: { (snapshot) in
        if let dictionary = snapshot.value as? [String: AnyObject] {
            let store = Store()
            store.Latitude = dictionary["Latitude"]?.doubleValue
            store.Longitude = dictionary["Longitude"]?.doubleValue
            store.Store = dictionary["Store"] as? String
            store.Status = dictionary["Status"] as? String
            stores.append(store)

            DispatchQueue.main.async {
                self.performSegue(withIdentifier: "LogInToMain", sender: nil)
            }
        }
    })

And on the next ViewController, i use the date to make annotations on a map. Like this:

func createAnnotation(Latitude:Double, Longitude:Double, Store:String, Status:String) {
    let annotation = CustomPointAnnotation()
    let latitude: CLLocationDegrees = Latitude
    let longitude: CLLocationDegrees = Longitude
    annotation.coordinate = CLLocationCoordinate2DMake(latitude, longitude)
    annotation.title = Store
    annotation.imageName = "\(Status).png"
    map.addAnnotation(annotation)
}

But the problem is, that the data to the annotation dont change with the database anymore. The app needs to be opened, then closed, then opened again before the data is shown correctly. Can anyone help with that problem?

KENdi
  • 7,576
  • 2
  • 16
  • 31
Lasse Bickmann
  • 195
  • 3
  • 14
  • I'm working through issues with Firebase myself at the moment. I think you might find that your observe block gets called multiple times with persistence enabled. The first time will return the value from the local store, the second from the server. – Tim Edwards Jan 09 '18 at 10:03
  • https://firebase.google.com/docs/database/admin/retrieve-data This event is triggered once for each existing child and then again every time a new child is added to the specified path. I would use .childChanged or .value in the next VC. Btw @PeterHaddad mentions observeSingleEventOfType, but in my experience, that will most definitely not get you the latest result in most cases. – Peter de Vries Jan 10 '18 at 12:40

1 Answers1

3

Since you are using:

Database.database().isPersistenceEnabled = true

then you need to use keepSynced(true) so the data will stay synchronized.

Persistence Behavior:

By enabling persistence, any data that the Firebase Realtime Database client would sync while online persists to disk and is available offline, even when the user or operating system restarts the app. This means your app works as it would online by using the local data stored in the cache. Listener callbacks will continue to fire for local updates.

keepSynced(true):

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.

for more info check this: https://firebase.google.com/docs/database/ios/offline-capabilities

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • Thank you! I have added: (to viewDidLoad() on the initial ViewController) let storesRef = Database.database().reference(withPath: "Stores") storesRef.keepSynced(true) But the problem is still there :'( – Lasse Bickmann Jan 09 '18 at 10:21
  • Did you find any solution? – dmyma Feb 26 '19 at 21:57
  • from where we should call Database.database().isPersistenceEnabled = true .....in func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {} OR ? – YodagamaHeshan Mar 31 '20 at 04:43