菜鸟教程小白 发表于 2022-12-11 19:13:27

ios - 使用firestore(firebase云数据库)获取重复数据时


                                            <p><p>我正在使用具有设置限制的 firestore 数据在 tableview 中加载更多功能,同时我滚动 tableview 获取 addListener 方法调用,我检查了两次,得到重复记录。 </p>

<p>在下面的代码中,我已经打印了我得到的快照。每次滚动时,它都会按快照 ID 及其数据打印。</p>

<p>仍然得到重复的数据。 firebase 控制台是否有任何其他设置?</p>

<p>这是我的代码,但代码中没有任何错误,它来自 firebase。</p>

<pre><code>func getTasks(){

    // self.tableViewData.reloadDataWithLoader(true)
    let userID = HelperFunction.helper.FetchFromUserDefault(name: kUID)
    let taskRef = Firestore.firestore().collection(&#34;tasks&#34;)
    let query :Query

    if (self.snapLastkey == &#34;&#34;) {
      self.arrayTask = []
      self.arrayKeys = []
      self.arraySelectedTask = []
      query = taskRef.whereField(kUUID, isEqualTo: userID).whereField(kIsCompleted, isEqualTo: false).limit(to: kLimit)
    }else{
      query = taskRef.whereField(kUUID, isEqualTo: userID).whereField(kIsCompleted, isEqualTo: false).limit(to: kLimit).start(afterDocument: self.snapLastDocument!)
    }

    query.addSnapshotListener { (snapshot, error) in

      if(error == nil){

            guard let snapshot = snapshot else {
                // The query snapshot has error.
                runOnMainThread {
                  // self.tableViewData.reloadData(withPlaceholderString: &#34;No task found.&#34;, with: APP_BLACK_COLOR)
                }
                print(&#34;Error retrieving products: \(error.debugDescription)&#34;)
                return
            }

            guard let lastSnapshot = snapshot.documents.last else {
                // The collection is empty.
                runOnMainThread {
                  self.tableViewData.reloadData(withPlaceholderString: &#34;No task found.&#34;, with: APP_BLACK_COLOR)
                  if(self.snapLastkey != &#34;&#34;) {
                        self.tableViewData.reloadDataWithLoader(false)
                        self.footerw.endRefreshingWithNoMoreData()

                  }
                }

                return
            }

         // self.snapLastDocument = lastSnapshot

            if (self.arrayTask.count==0) {
                self.arrayTask = []
                self.arrayKeys = []
                self.arraySelectedTask = []
            }




            snapshot.documentChanges.forEach({ (document) in
                print(&#34;\(document.document.documentID) =&gt; \(document.document.data())&#34;)
                let json = JSON(document.document.data())
                self.arrayTask.append(Task.init(json: json))
                self.arraySelectedTask.append(false)
                self.arrayKeys.append(document.document.documentID)
                self.snapLastkey = document.document.documentID as String
                self.snapLastDocument = document.document

            })

            print(&#34;\n\n\n\n\n--------arr print---------\n\n&#34;)
            fori in 0..&lt;self.arrayTask.count
            {
                let task = self.arrayTask
                print(&#34;arr task Title:&#34;,task.title)
            }


            self.tableViewData.reloadData()
            if(self.arrayTask.count==0){
                self.snapLastkey = &#34;&#34;
                self.tableViewData.reloadData(withPlaceholderString: &#34;No task found.&#34;, with: APP_BLACK_COLOR)
            }else{
                if Int(snapshot.documents.count) &lt; kLimit{
                  self.snapLastkey = &#34;&#34;
                  self.footerw.endRefreshingWithNoMoreData()
                }
                else{
                  self.footerw.endRefreshing()
                }

            }
            print(self.arrayTask)

      }else{
            print(error?.localizedDescription ?? &#34;&#34;)
            self.tableViewData.reloadData(withPlaceholderString: &#34;Try again.&#34;, with: APP_BLACK_COLOR)
      }


    }

}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p><strong>已解决</strong></p>

<p>它只是<strong>索引问题</strong>。我在日志中找到了它。所以在日志中我得到了在 firebase 索引部分创建索引的链接。我已经创建了需要的新索引,我的数据即将到来正是我想要的。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 使用firestore(firebase云数据库)获取重复数据时,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/47156847/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/47156847/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 使用firestore(firebase云数据库)获取重复数据时