通過使用查詢而不是反復觀察單個事件,加快為我的社交網絡應用程序提取帖子我有一系列的鍵導致我的社交網絡的帖子對象像/ posts / id /(發布信息)當我加載帖子時,我使用該observeSingleEventOfType(.Value)方法加載/發布/ 0然后/ posts / 1等。我使用a一次lazyTableView加載30并且速度很慢。有沒有什么方法可以使用其中一種查詢方法或其他方法使其更快,即使我必須重構我的JSON樹中的數據。我來自Parse重新實現我的應用程序,到目前為止,經驗非常好。只有這一點我有點堅持。在此先感謝您的幫助!編輯:func loadNext(i: Int) {
// check if exhists let ideaPostsRef = Firebase(url: "https://APPURL")
ideaPostsRef.childByAppendingPath(i.description).observeSingleEventOfType(.Value, withBlock: {
(snapshot) in
if i % 29 == 0 && i != 0 && !self.hitNull { return }
// false if nil // true if not nil if !(snapshot.value is NSNull) {
let postJSON = snapshot.value as! [String: AnyObject]
print("GOT VALID \(postJSON)")
let post = IdeaPost(message: postJSON["message"] as! String, byUser: postJSON["user"] as! String, withId: i.description)
post.upvotes = postJSON["upvotes"] as! Int
self.ideaPostDataSource.append(post)
self.loadNext(i + 1)
} else {
// doesn't exhist print("GOT NULL RETURNING AT \(i)")
self.doneLoading = true
self.hitNull = true
return
}
}}這個遞歸函數基本上從firebase獲取密鑰號i的值。如果它是NSNULL,它知道這是最后一個可能加載的帖子,永遠不會再做。如果NSNULL沒有被擊中但是i % 29 == 0它以基本情況返回,所以一次只加載30個帖子(0索引)。當我設置doneLoading為時true,tableView.reloadData()使用屬性觀察者調用。這是我正在獲取的數組的樣本"ideaPosts" : [ {
"id" : 0,
"message" : "Test",
"upvotes" : 1,
"user" : "Anonymous"
}, {
"id" : 1,
"message" : "Test2",
"upvotes" : 1,
"user" : "Anonymous"
} ]
- 1 回答
- 0 關注
- 462 瀏覽
添加回答
舉報
0/150
提交
取消
