[英]How do you properly order data from Firebase chronologically
我正在嘗試從Firebase訂購我的數據,因此最新的帖子位於頂部(如Instagram),但我無法讓它正常工作。 我應該使用服務器時間戳嗎? 是否有“createdAt”字段?
func getPosts() {
POST_REF.observeEventType(.Value, withBlock: { snapshot in
guard let posts = snapshot.value as? [String : [String : String]] else {
print("No Posts Found")
return
}
Post.feed?.removeAll()
for (postID, post) in posts {
let newPost = Post.initWithPostID(postID, postDict: post)!
Post.feed?.append(newPost)
}
Post.feed? = (Post.feed?.reverse())!
self.tableView.reloadData()
}, withCancelBlock: { error in
print(error.localizedDescription)
})
}
僅對數組使用reverse()不足以涵蓋所有內容。 您需要考慮以下不同的事情:
在檢索數據時限制,使用append()然后reverse()來節省時間。 您不需要每次都刪除所有數組。
滾動觸發器或willDisplay單元格方法加載
開始吧。 您可以為帖子時間戳或全局日期/時間創建子項。 為了提供類似Instagram的幾秒鍾,我建議你使用UTC時間 。 所以我會這樣稱呼:( timeUTC )
要對所有帖子進行排序,請使用自1970年以來的時間戳。 因此,我將調用此( 時間戳 ),然后您還可以將另一個節點作為(reversedTimestamp)添加 - 前綴到時間戳。 所以當你對這個節點使用queryOrdered時。 您可以使用yourQuery.queryLimited(toFirst: 5)
處理最新的5個帖子。
1.獲取Swift 3中timeUTC節點的UTC日期/時間:
let date = Date()
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZ"
formatter.timeZone = TimeZone(abbreviation: "UTC")
let utcTimeZoneStr = formatter.string(from: date)
+0000意味着它是世界時,請看http://time.is/tr/UTC
2.獲取自1970年時間戳以對Swift 3中的帖子進行排序:
let timestamp = (Date().timeIntervalSince1970 as NSString).doubleValue
let reversedTimestamp = -1.0 * timestamp
現在,您可以將它們保存在Firebase帖子上。
"posts" : {
"-KHLOy5mOSq0SeB7GBXv" : {
"timestamp": "1475858019.2306"
"timeUTC" : "2012-02-04 12:11:56 +0000"
},
"-KHLrapS0wbjqPP5ZSUY" : {
"timestamp": "1475858010.1245"
"timeUTC" : "2014-02-04 12:11:56 +0000"
},
我將檢索五個五個帖子,所以我在viewDidLoad
執行queryLimited(toFirst:5) :
let yourQuery = ...queryOrdered(byChild: "reverseTimestamp")
.queryEnding(atValue: "\(self.pageOnTimestamp)", childKey: "reverseTimestamp")
yourQuery.observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.value is NSNull {
print("There is no post.")
}
else {
yourQuery.queryLimited(toFirst: 5).observeSingleEvent(of: .value, with: { (snapshot) in
self.posts.removeAll(keepingCapacity: true)
for (i, snap) in snapshot.children.enumerated() {
if let postAllDict = snapshot.value as? [String: AnyObject] {
if let postDict = postAllDict[(snap as AnyObject).key as String] as? [String: AnyObject] {
let post = Post(key: (snap as AnyObject).key as String, postDict: postDict)
self.posts.append(post)
}
}
}
completion(true)
})
}
})
如果用戶到達最新帖子,您可以使用如下所示的willDisplay
方法處理它,然后您可以調用loadMore函數。
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if self.posts.count - 1 == indexPath.row {
// call loadMore function.
}
}
在loadMore()函數中,您可以處理最新帖子的時間戳,然后是開始結束查詢,這樣您就可以輕松地繼續下一個前5個帖子,同時在數組之前附加。
對於格式良好的Swift 3轉換,請看一下: Swift 3 - UTC時間標簽,思考12h / 24h設備時間變化
根據@tobeiosdev的答案我定義我的數據結構如下:
"posts" : {
"-KcOa8GXBl08V-WXeX8P" : {
"author" : "@giovanny.piñeros",
"hashtags" : [ "Hello", "World" ],
"text" : "Hola Mundo!!!!!",
"timestamp" : 5.08180914309278E8,
"title" : "Hello World",
"userid" : "hmIWbmQhfgh93"
}
正如您所看到的,我添加了一個timestamp屬性,當我使用子添加事件查詢我的數據時,我將該數據傳遞給post對象,然后我將該對象附加到我的表視圖將從中提供的帖子數組:
self.posts.append(post)
self.posts.sort(by: {$0.timestamp! > $1.timestamp!})
self.tableViewFeed.reloadData()
通過排序方法,我設法按照從最新到最舊的帖子的期望順序來排序我的數據。 我希望這種方法可以幫助任何人:)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.