2017-03-07 65 views
2

我有两个问题与下面的代码片段。TableView重复和Firebase搜索查询

  1. 我在我的TableView中得到了重复。
  2. 当我寻找的东西,它返回一个结果,我试图寻找返回结果另一件事,现有的结果将是一个展示不是新的结果,例如:

    一个。我搜索“ola”,我的TableView返回一个 - > olabode,olatunde,olaide的列表。

    b。然后我搜索“bisi”,如果找到与“bisi”匹配的查询,我仍然得到“ola”的查询结果。

    func searchView(_ searchView: AZSearchViewController, didTextChangeTo text: String, textLength: Int) { 
        self.resultArray.removeAll() 
        searchView.reloadData() 
    
    guard text != "" else { 
        return 
    }  
        AppFirRef.userRef.queryOrdered(byChild: "username") 
         .queryStarting(atValue: text) 
         .queryEnding(atValue: text + "\\uf8ff") 
         .observe(.value, with: { (snapshot) in 
          if (snapshot.value is NSNull) { 
           print("not found") 
          } else { 
           print("found") 
           self.resultArray.removeAll() 
           searchView.reloadData() 
    
           print("\(snapshot.value)") 
           for case let snap as FIRDataSnapshot in snapshot.children { 
            guard let value = snap.value as? [String : Any] else { continue } 
            //print("\(snap.key)") 
            print(value) 
            let user = LContact(value: value, searchUserId: snap.key) 
            self.set.add(user) 
            if let username = user.username{ 
             self.resultArray.append(username.lowercased()) 
             searchView.reloadData() 
            } 
           } 
    
          } 
         }) 
    
    } 
    
+0

您是从TableViewSource中删除行在添加新数据之前? – apineda

+0

@apineda我注意到,例如当发生重复时,如果设备A正在添加到“用户”节点,它将导致设备B中用户列表中的重复,直到我在复制消失之前重新启动设备b,所以基本上,如果正在从其他设备引用“用户”节点,我的查询将返回重复 – SimpiMind

回答

1

它可能为时已晚,但变化

.observe(.value, with: { (snapshot) in 
... 
)} 

.observeSingleEvent(of: .value, with: { (snapshot) in 
... 
)} 

看到我的回答this question的解释;它也适用于你,因为每当参考路径上的数据发生更改时(例如,其他设备上的某个人改变了参考路径中的数据时,你也会追加到该阵列中)