通過(guò)使用查詢(xún)而不是反復(fù)觀察單個(gè)事件,加快為我的社交網(wǎng)絡(luò)應(yīng)用程序提取帖子我有一系列的鍵導(dǎo)致我的社交網(wǎng)絡(luò)的帖子對(duì)象像/ posts / id /(發(fā)布信息)當(dāng)我加載帖子時(shí),我使用該observeSingleEventOfType(.Value)方法加載/發(fā)布/ 0然后/ posts / 1等。我使用a一次lazyTableView加載30并且速度很慢。有沒(méi)有什么方法可以使用其中一種查詢(xún)方法或其他方法使其更快,即使我必須重構(gòu)我的JSON樹(shù)中的數(shù)據(jù)。我來(lái)自Parse重新實(shí)現(xiàn)我的應(yīng)用程序,到目前為止,經(jīng)驗(yàn)非常好。只有這一點(diǎn)我有點(diǎn)堅(jiān)持。在此先感謝您的幫助!編輯: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
}
}}這個(gè)遞歸函數(shù)基本上從firebase獲取密鑰號(hào)i的值。如果它是NSNULL,它知道這是最后一個(gè)可能加載的帖子,永遠(yuǎn)不會(huì)再做。如果NSNULL沒(méi)有被擊中但是i % 29 == 0它以基本情況返回,所以一次只加載30個(gè)帖子(0索引)。當(dāng)我設(shè)置doneLoading為時(shí)true,tableView.reloadData()使用屬性觀察者調(diào)用。這是我正在獲取的數(shù)組的樣本"ideaPosts" : [ {
"id" : 0,
"message" : "Test",
"upvotes" : 1,
"user" : "Anonymous"
}, {
"id" : 1,
"message" : "Test2",
"upvotes" : 1,
"user" : "Anonymous"
} ]
- 1 回答
- 0 關(guān)注
- 461 瀏覽
添加回答
舉報(bào)
0/150
提交
取消