1
Firebaseのタイムスタンプの子値に基づいてtableViewをソートしたいと思います。最新のtableViewCellをtableViewに追加して、tableViewがロード(またはリロード)するたびにtableviewの上に配置したいと思います。事前に助けてくれてありがとう! firebase最新のタイムスタンプに基づくload tableView
{
"users" : {
"CmtuebwVrmOG3n4uSsIK1b4FsSN2" : {
"Places" : {
"-KhDwZJvca9HedFluJ5O" : {
"addedBy" : "CmtuebwVrmOG3n4uSsIK1b4FsSN2",
"place" : "Edinburgh, UK",
"timestamp" : 1.491678152020824E9
},
"-KhE7raDrM2RRs-N6FXg" : {
"addedBy" : "CmtuebwVrmOG3n4uSsIK1b4FsSN2",
"place" : "Hong Kong",
"timestamp" : 1.49168137667081E9
},
"-KhFUaEjjhE3RBOw95fc" : {
"addedBy" : "CmtuebwVrmOG3n4uSsIK1b4FsSN2",
"place" : "Illinois, USA",
"timestamp" : 1.491704112134812E9
},
"-KhzSIHrFHiUEBrzBKUH" : {
"addedBy" : "CmtuebwVrmOG3n4uSsIK1b4FsSN2",
"place" : "Ghana",
"timestamp" : 1.492492039376661E9
}
},
//から
// JSONは、あなたは自分の配列の第一インデックスに挿入される最新のデータレコードをしたい場合は場所
var placeList = [Place]()
var placesDictionary = [String: Place]()
func fetchPlaces() {
let uid = FIRAuth.auth()?.currentUser?.uid
let ref = FIRDatabase.database().reference().child("users").child(uid!).child("Places")
ref.observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let place = Place()
place.setValuesForKeys(dictionary)
// self.placeList.append(place)
if let addedBy = place.addedBy {
self.placesDictionary[addedBy] = place
self.placeList.append(place)
self.tableView.beginUpdates()
self.tableView.insertRows(at: [IndexPath(row: self.placeList.count-1, section: 0)], with: .automatic)
self.tableView.endUpdates()
}
//this will crash because of background thread, so lets call this on dispatch_async main thread
DispatchQueue.main.async(execute: {
self.tableView.reloadData()
})
}
}, withCancel: nil)
}
感謝を!はい - 開始と終了の更新コードは私にとっても重複しているようでした。 – user3708224
問題が解決した場合は、心配する必要はありません。 – Lawliet