我启用了FIRDatabase.database()。persistenceEnabled = true,因此即使在离线模式下,缓存的数据也会保留。但是,我确实意识到,即使有数据更改,应用也拒绝更新并添加新内容。
例如,这是我的json树:
{
"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"
}
}
}
当条目被添加到" palettes"时,即使重新启动它,应用程序也不会更新。即使我们在仍然保持离线功能的同时不重启应用程序,我如何启用它会持续监听和更新?
这是我从#34; 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
})
}
我理解可能是因为我使用observeSingleEventType因此阻止它听取真正的更新。现在它只从本地缓存中提取数据。
编辑:以这种方式添加它是对的吗?
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
})
}