Firebase重复单元格

时间:2018-05-22 12:13:58

标签: swift uitableview firebase firebase-realtime-database

在从数据库导入客户到tableview期间,是否可以避免重复具有相同名称内容的单元格?在我的例子中,如果客户Ben Smith有两个有价值观的孩子,我只想要一个名字的单元格。

这是我的数据库结构......

enter image description here

导致tableview:

enter image description here

let userID = Auth.auth().currentUser!.uid
    let usersDatabaseRef = Database.database().reference().child("usersDatabase").child(userID).child("Customers")
    usersDatabaseRef.observe(.value, with: { snapshot in
        print("there are \(snapshot.childrenCount) users")
        for child in snapshot.children {
            let childSnap = child as! DataSnapshot
            print("user: \(childSnap.key)")
            let userCustomerSnap = childSnap
            for customer in userCustomerSnap.children.allObjects as! [DataSnapshot] {
                let customerSnap = customer
                let dict = customerSnap.value as! [String: Any]
                let name = dict["Name and surname"]
                let phone = dict["Phone"]
                let company = dict["Company name"]
                let customerID = dict["ID"]
                let email = dict["Email"]
                let nip = dict["Nip1"]
                let postal = dict["Postal code"]
                let street = dict["Street"]
                let town = dict["Town"]
                let myCustomer = CustomerModel(name: name as? String, phone: phone as? String, company: company as? String, customerID: customerID as? String, email: email as? String, nip: nip as? String, postal: postal as? String, street: street as? String, town: town as? String)
                self.candies.append(myCustomer)
                self.filteredCandies.append(myCustomer)
            }
            self.tableViewCustomer.reloadData()

2 个答案:

答案 0 :(得分:0)

你真的不需要那个for循环。这可能是导致您发出问题的原因。无论如何,代码的调用次数与您有孩子的次数相同,因此您不需要该循环。请尝试下面的代码。如果这不起作用,您可能需要尝试.childAdded而不是.value

guard let userID = Auth.auth().currentUser?.uid else { return }
let usersDatabaseRef = Database.database().reference().child("usersDatabase").child(userID).child("Customers")
usersDatabaseRef.observe(.childAdded, with: { snapshot in

    guard let dict = snapshot.value as? [String: Any] else { return }

    let name = dict["Name and surname"] as? String
    let phone = dict["Phone"] as? String
    let company = dict["Company name"] as? String
    let customerID = dict["ID"] as? String
    let email = dict["Email"] as? String
    let nip = dict["Nip1"] as? String
    let postal = dict["Postal code"] as? String
    let street = dict["Street"] as? String
    let town = dict["Town"] as? String

    let myCustomer = CustomerModel(name: name, phone: phone, company: company, customerID: customerID, email: email, nip: nip, postal: postal, street: street, town: town)

    self.candies.append(myCustomer)

    self.filteredCandies.append(myCustomer)

    DispatchQueue.main.async {
        self.tableViewCustomer.reloadData()
    }

}, withCancel: nil)

答案 1 :(得分:0)

每次您从该客户处找到订单(或您的数据中“Ben Smith”所代表的级别)时,您都会在列表中添加项目。所以你的清单是订单清单,而不是客户清单。

通常在NoSQL / Firebase中,建议您为要显示的内容建模数据库。因此,如果您想要一个客户列表,那就是我在数据库中存储的内容。但是根据您的数据结构,您还可以在代码中修复它:

usersDatabaseRef.observe(.value, with: { snapshot in
    print("there are \(snapshot.childrenCount) users")
    for child in snapshot.children {
        let childSnap = child as! DataSnapshot
        let myCustomer = CustomerModel(name: child.key, phone: "", company: "", customerID: "", email: "", nip: "", postal: "", street: "", town: ")
        self.candies.append(myCustomer)
        self.filteredCandies.append(myCustomer)
    }

    self.tableViewCustomer.reloadData()
})

或者,只有当新客户的名称与您之前看到的订单不同时才添加新客户:

var previousName: String = ""
usersDatabaseRef.observe(.value, with: { snapshot in
    print("there are \(snapshot.childrenCount) users")
    for child in snapshot.children {
        let childSnap = child as! DataSnapshot
        print("user: \(childSnap.key)")
        let userCustomerSnap = childSnap
        for customer in userCustomerSnap.children.allObjects as! [DataSnapshot] {
            let customerSnap = customer
            let dict = customerSnap.value as! [String: Any]
            let name = dict["Name and surname"]
            if name != previousName {
                let phone = dict["Phone"]
                let company = dict["Company name"]
                let customerID = dict["ID"]
                let email = dict["Email"]
                let nip = dict["Nip1"]
                let postal = dict["Postal code"]
                let street = dict["Street"]
                let town = dict["Town"]
                let myCustomer = CustomerModel(name: name as? String, phone: phone as? String, company: company as? String, customerID: customerID as? String, email: email as? String, nip: nip as? String, postal: postal as? String, street: street as? String, town: town as? String)
                self.candies.append(myCustomer)
                self.filteredCandies.append(myCustomer)
                previousName = name
            }
        }
    }
    self.tableViewCustomer.reloadData()