从firebase获取用户的数据?

时间:2017-01-24 16:08:06

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

目前我以这种方式显示用户的联系人

 $('a').replaceWith(function(){
       return $(this).contents();
 });

如何仅显示与添加了列的用户具有相同电子邮件的用户? 当您创建新联系人时,它会自动将您的电子邮件的值分配给addedByUser。 因此,例如,如果我从帐户show@mycontact.com添加联系人,它将仅显示具有增值值的联系人用户:"显示@ mycontact"

enter image description here

1 个答案:

答案 0 :(得分:2)

更新的答案 - 有两个选项: - )

    let ref = FIRDatabase.database().reference(withPath: "contacts")

    var newItems : [ContactItem] = []

    ref.observe(.value, with: { snapshot in

        for item in snapshot.children
        {
            let item = ContactItem(snapshot: item as! FIRDataSnapshot)
            newItems.append(item)
        }

        let userID = FIRAuth.auth()?currentUser?.email

        let itemsMatching  = newItems.filter
        {
            $0.addedByUser == userID
        }

        let sortedNames = itemsMatching.sorted { $0.lastName < $1.lastName }

        print(sortedNames)
    })

另一方面,假设你正在迭代快照,你总是可以采用简单的方法

    let ref = FIRDatabase.database().reference(withPath: "contacts")

    var newItems : [ContactItem] = []
    let userID = FIRAuth.auth()?currentUser?.email

    ref.observe(.value, with: { snapshot in

        for item in snapshot.children
        {
            let item = ContactItem(snapshot: item as! FIRDataSnapshot)
            if item.addedByUser == userID
            {
                newItems.append(item)
            }
        }

        let sortedNames = newItems.sorted { $0.lastName < $1.lastName }

        print(sortedNames)
    })