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
})
}