值保留在数组中

时间:2017-04-28 04:10:20

标签: ios swift uitableview firebase firebase-realtime-database

我创建了一个功能,根据他们在" firstLocation"," secondLocation"," thirdLocation&中设置的值,显示用户特定人员#34 ;.我通过了所有Firebase用户,过滤并提取了匹配的所有帐户。这就是我的方式

func fetchUser(){
    FIRDatabase.database().reference().child("Users").observe(.childAdded, with: {(snapshot) in
        if let dictionary = snapshot.value as? [String: AnyObject]{
            let user = User()
            user.id = snapshot.key
            user.setValuesForKeys(dictionary)
            user.name = dictionary["name"] as! String?
            user.aboutMe = dictionary["aboutMe"] as! String?
            user.profileImageUrl = dictionary["profileImageUrl"] as! String?
            user.firstLocation = dictionary["firstLocation"] as! String?
            user.secondLocation = dictionary["secondLocation"] as! String?
            user.thirdLocation = dictionary["thirdLocation"] as! String?
            user.guiderMode = dictionary["guiderMode"] as! String?
            self.users =  self.users.filter{(($0.firstLocation?.contains(self.passedLocation!))!)&&$0.guiderMode == "True" || (($0.secondLocation?.contains(self.passedLocation!))!) || (($0.thirdLocation?.contains(self.passedLocation!))!)}
            self.users.append(user)
            print("The count of the users", self.users.count as Any)

            self.tableView.reloadData()
        }

    }, withCancel:nil)
}

现在的问题是,由于某些原因,一个值总是会出现,即使它不应该被看到,因为它不符合过滤器要求。我试着通过

来修复它
users.removeAll()
tableView.reloadData()

但无论什么

,它仍会显示出来

1 个答案:

答案 0 :(得分:0)

查看self.users.append(user)。您总是在不对其应用过滤器的情况下附加新用户,以便在调用observe回调后用户总是至少有1个元素。