ios - 通过使用查询而不是重复观察单个事件来加快获取我的社交网络应用程序的帖子
<p><p>我有一组键,这些键可以为我的社交网络发布对象,例如/posts/id/(post info) </p>
<p>当我加载帖子时,我使用 <code>observeSingleEventOfType(.Value)</code> 方法加载/posts/0 然后/posts/1 等。 </p>
<p>我使用 <code>lazyTableView</code> 一次加载 30 个,而且速度很慢。有什么方法可以使用其中一种查询方法或另一种使其更快的方法,即使我必须重组 JSON 树中的数据。 </p>
<p>我来自 Parse,正在重新实现我的应用程序,到目前为止体验非常好。只是这一件事我有点坚持。提前感谢您的帮助!</p>
<p><strong>编辑:</strong></p>
<pre><code>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!
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
}
}
}
</code></pre>
<p>这个递归函数本质上是从 firebase 获取键号 i 的值。如果它是 NSNULL 它知道这是最后可能加载的帖子并且永远不会再加载。如果 NSNULL 没有被命中但 <code>i % 29 == 0</code> 则它作为基本情况返回,因此一次只加载 30 个帖子(0 索引)。当我将 <code>doneLoading</code> 设置为 <code>true</code> 时,会使用属性观察器调用 <code>tableView.reloadData()</code>。 </p>
<p>这是我正在获取的数组的示例</p>
<pre><code>"ideaPosts" : [ {
"id" : 0,
"message" : "Test",
"upvotes" : 1,
"user" : "Anonymous"
}, {
"id" : 1,
"message" : "Test2",
"upvotes" : 1,
"user" : "Anonymous"
} ]
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p><em>更新:我们现在也在 <a href="https://youtu.be/66lDSYtyils?t=1m49s" rel="noreferrer noopener nofollow">AskFirebase episode</a> 中讨论了这个问题。 .</em></p>
<p>从 Firebase 加载许多项目不必很慢,因为您可以通过管道传输请求。但是您的代码使这成为不可能,这确实会导致性能欠佳。</p>
<p>在您的代码中,您从服务器请求一个项目,等待该项目返回,然后加载下一个项目。在一个简化的序列图中,如下所示:</p>
<pre><code>Your app Firebase
Database
-- request item 1 -->
SL
eo
ra
vd
ei
<-return item1 --rn
g
-- request item 2 -->
SL
eo
ra
vd
ei
rn
<-return item2 -- g
-- request item 3 -->
.
.
.
-- request item 30-->
SL
eo
ra
vd
ei
rn
g
<-return item 30 --
</code></pre>
<p>在这种情况下,您正在等待 30 倍的往返时间 + 30 倍的从磁盘加载数据所需的时间。如果(为了简单起见)我们说往返需要 1 秒,从磁盘加载项目也需要 1 秒,那么至少要 30 * (1 + 1) = 60 秒。</p>
<p>在 Firebase 应用程序中,如果您一次性发送所有请求(或至少合理数量的请求),您将获得更好的性能:</p>
<pre><code>Your app Firebase
Database
-- request item 1 -->
-- request item 2 -->SL
-- request item 3 -->eo
. ra
. vd
. ei
-- request item 30-->rn
g
<-return item1 --
<-return item2 --
<-return item3 --
.
.
.
<-return item 30 --
</code></pre>
<p>如果我们再次假设 1 秒往返和 1 秒加载,您将等待 30*1 + 1 = 31 秒。</p>
<p>所以:所有请求都通过同一个连接。鉴于此,<code>get(1)</code>、<code>get(2)</code>、<code>get(3)</code> 和 <code>getAll()</code> 是帧的一些开销。</p>
<p>我设置了 <a href="http://jsbin.com/mocona/edit?js,console" rel="noreferrer noopener nofollow">jsbin to demonstrate the behavior</a> .数据模型非常简单,但它展示了差异。 </p>
<pre><code>function loadVideosSequential(videoIds) {
if (videoIds.length > 0) {
db.child('videos').child(videoIds).once('value', snapshot => {
if (videoIds.length > 1) {
loadVideosSequential(videoIds.splice(1), callback)
}
});
}
}
function loadVideosParallel(videoIds) {
Promise.all(
videoIds.map(id => db.child('videos').child(id).once('value'))
);
}
</code></pre>
<p>为了比较:在我的系统上顺序加载 64 个项目需要 3.8 秒,而通过管道加载它们(就像 Firebase 客户端 native 那样)需要 600 毫秒。确切的数字取决于您的连接(延迟和带宽),但流水线版本应该总是更快。</p></p>
<p style="font-size: 20px;">关于ios - 通过使用查询而不是重复观察单个事件来加快获取我的社交网络应用程序的帖子,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/39986748/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/39986748/
</a>
</p>
页:
[1]