菜鸟教程小白 发表于 2022-12-12 13:01:54

ios - 在 Firebase 中获取有限数量的记录


                                            <p><p>我想在 Firebase 中查询符合我的条件的记录,但我只想获取其中的几个。我正在使用 swift 。这可能吗?例如,我有一个这样的查询:</p>

<pre><code>firebaseReference.child(&#34;array&#34;).queryOrdered(byChild: &#34;userType&#34;).queryEqual(toValue: &#34;family&#34;).observe(.value, with: { (snapshotVec) -&gt; Void in
            Storage.shared.numFamsInVec = snapshotVec.childrenCount
}
</code></pre>

<p>这将检索 <code>userType</code> 为 <code>family</code> 的所有记录,但假设我只想获取其中的一半。我该怎么做?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>确实如此。魔法就在<a href="https://firebase.google.com/docs/database/ios/lists-of-data#filtering_data" rel="noreferrer noopener nofollow">Firebase documentation for filtering data on iOS</a> :</p>

<pre><code>firebaseReference
    .child(&#34;array&#34;)
    .queryOrdered(byChild: &#34;userType&#34;)
    .queryEqual(toValue: &#34;family&#34;)
    .queryLimited(toFirs: 10)
    .observe(.value, with: { (snapshotVec) -&gt; Void in
            Storage.shared.numFamsInVec = snapshotVec.childrenCount
    }
</code></pre>

<p>另见 <a href="https://firebase.google.com/docs/reference/ios/firebasedatabase/api/reference/Classes/FIRDatabaseQuery#-querylimitedtofirst" rel="noreferrer noopener nofollow">Firebase reference documentation</a> ,其中包含 Swift 3 的魔法咒语。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在 Firebase 中获取有限数量的记录,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42354750/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42354750/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在 Firebase 中获取有限数量的记录