菜鸟教程小白 发表于 2022-12-11 17:49:13

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: &#34;https://APPURL&#34;)

    ideaPostsRef.childByAppendingPath(i.description).observeSingleEventOfType(.Value, withBlock: {
      (snapshot) in

      if i % 29 == 0 &amp;&amp; i != 0 &amp;&amp; !self.hitNull { return }
            // false if nil
            // true if not nil
      if !(snapshot.value is NSNull) {
            let postJSON= snapshot.value as!
            print(&#34;GOT VALID \(postJSON)&#34;)
            let post = IdeaPost(message: postJSON[&#34;message&#34;] as! String, byUser: postJSON[&#34;user&#34;] as! String, withId: i.description)
            post.upvotes = postJSON[&#34;upvotes&#34;] as! Int
            self.ideaPostDataSource.append(post)
            self.loadNext(i + 1)
      } else {
            // doesn&#39;t exhist
            print(&#34;GOT NULL RETURNING AT \(i)&#34;)
            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>&#34;ideaPosts&#34; : [ {
    &#34;id&#34; : 0,
    &#34;message&#34; : &#34;Test&#34;,
    &#34;upvotes&#34; : 1,
    &#34;user&#34; : &#34;Anonymous&#34;
}, {
    &#34;id&#34; : 1,
    &#34;message&#34; : &#34;Test2&#34;,
    &#34;upvotes&#34; : 1,
    &#34;user&#34; : &#34;Anonymous&#34;
} ]
</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 --&gt;
                               SL
                               eo
                               ra
                               vd
                               ei
      &lt;-return item1 --rn
                                  g
      -- request item 2 --&gt;
                               SL
                               eo
                               ra
                               vd
                               ei
                               rn
      &lt;-return item2 --   g
      -- request item 3 --&gt;
               .
               .
               .
      -- request item 30--&gt;
                               SL
                               eo
                               ra
                               vd
                               ei
                               rn
                                  g
      &lt;-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 --&gt;
      -- request item 2 --&gt;SL
      -- request item 3 --&gt;eo
               .             ra
               .             vd
               .             ei
      -- request item 30--&gt;rn
                                  g
      &lt;-return item1 --   
      &lt;-return item2 --      
      &lt;-return item3 --
               .
               .
               .
      &lt;-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 &gt; 0) {
    db.child(&#39;videos&#39;).child(videoIds).once(&#39;value&#39;, snapshot =&gt; {
      if (videoIds.length &gt; 1) {
      loadVideosSequential(videoIds.splice(1), callback)
      }
    });
}
}

function loadVideosParallel(videoIds) {
Promise.all(
    videoIds.map(id =&gt; db.child(&#39;videos&#39;).child(id).once(&#39;value&#39;))
);
}
</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]
查看完整版本: ios - 通过使用查询而不是重复观察单个事件来加快获取我的社交网络应用程序的帖子