0

I've enabled FIRDatabase.database().persistenceEnabled = true and thus data that's cached will remain even in offline mode. However, I did realise that even when there are data changes, the app refuses to update and add the new content in.

for example, here's my json tree:

{
  "palettes" : {
    "-KTsjCh8nMErdbNXuHqL" : {
      "URL" : --,
      "accessories" : "#141615",
      "bottom" : "#BA9977",
      "overall" : "#676769",
      "shoes" : "#FFFFFF",
      "thisSeason" : "true",
      "title" : "test",
      "top" : "#000000"
    },
    "-KTsjCnNWoUNKoYBlH-j" : {
      "URL" : --,
      "accessories" : "#141615",
      "bottom" : "#BA9977",
      "overall" : "#676769",
      "shoes" : "#FFFFFF",
      "thisSeason" : "false",
      "title" : "test1",
      "top" : "#000000"
    }
  }
}

When entries are added to "palettes", the app does not update even when I restart it. How do I enable such that it will continuously listen and update even if we do not restart the app while still maintaining offline capabilities?

here's my function to gather data from "palettes"

func generateDataForRecents()  {
        if URLArrayStringThisSeason.count == 0 {
            self.activityIndicator2.isHidden = false
            self.activityIndicator2.startAnimating()
        }


        let databaseRef = FIRDatabase.database().reference()

        databaseRef.child("palettes").queryLimited(toFirst: 100).observeSingleEvent(of: .value, with: { (snapshot) in

            if let snapDict = snapshot.value as? [String:AnyObject]{

                for each in snapDict as [String:AnyObject]{

                    let URL = each.value["URL"] as! String

                    self.URLArrayStringRecents.append(URL)
                    //print(self.URLArrayString.count)

                    //print(snapshot)

                    //let pictureTitle = each.value["title"] as! String

                }
            }

            self.whatsNewCollectionView?.reloadData() //Reloads data after the number and all the URLs are fetched
            self.activityIndicator2.stopAnimating()
            self.activityIndicator2.isHidden = true
        })
    }

I understand it could be the fact that I'm using observeSingleEventType hence preventing it from listening to real updates. Right now it only pulls data from the local cache.

edit: is it right of me to add it in such a way?

 func generateDataForRecents()  {
        if URLArrayStringThisSeason.count == 0 {
            self.activityIndicator2.isHidden = false
            self.activityIndicator2.startAnimating()
        }


        let databaseRef = FIRDatabase.database().reference()

        databaseRef.queryLimited(toFirst: 100).observe(.childAdded, with: { (snapshot) in

            if let snapDict = snapshot.value as? [String:AnyObject]{

                for each in snapDict as [String:AnyObject]{

                    let URL = each.value["URL"] as! String

                    self.URLArrayStringRecents.append(URL)
                    //print(self.URLArrayString.count)

                    //print(snapshot)

                    //let pictureTitle = each.value["title"] as! String

                }
            }

            self.whatsNewCollectionView?.reloadData() //Reloads data after the number and all the URLs are fetched
            self.activityIndicator2.stopAnimating()
            self.activityIndicator2.isHidden = true
        })


    }
Ler Ws
  • 317
  • 5
  • 17
  • Are you asking how to listen for updates? You'd do that by using `observeEvent` instead of `observeSingleEvent`. See https://firebase.google.com/docs/database/ios/read-and-write#listen_for_value_events – Frank van Puffelen Oct 13 '16 at 10:40
  • Yes but I still do not really understand how does persistenceEnabled work. When I have it turned on, why won't it scan the database for any changes and then add on top of the current cached data. Say I've retrieved the data and stored this in an array [1, 2] but now there's a new entry called "3" added. Why won't it update and append to the existing array instead of just redownloading the entire array when persistence is not enabled and not updating at all when persistence is enabled. – Ler Ws Oct 13 '16 at 10:43
  • I'll find an answer where I've explained how it works in the past. But in general the thing to remember is that disk persistence and `observeSingleEvent` don't mix well, unless you also use [keep the data synchronized](https://firebase.google.com/docs/database/ios/offline-capabilities#section-prioritizing-the-local-cache). – Frank van Puffelen Oct 13 '16 at 11:02
  • See http://stackoverflow.com/questions/34486417/firebase-offline-capabilities-and-addlistenerforsinglevalueevent/34487195#34487195 – Frank van Puffelen Oct 13 '16 at 11:07
  • Hmm thanks, I'll have a read. Another question: is it true that with .observe you can't do query equal to value? Somehow when I'm using .observe I can't use databaseRef.child("palettes") – Ler Ws Oct 13 '16 at 11:25

0 Answers0