仅在符合当前用户数据的情况下才提取用户数据 [英] Fetch users data only if it matches current users data

查看:140
本文介绍了仅在符合当前用户数据的情况下才提取用户数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个tableView,可以提取Firebase数据库中的所有用户。 tableView单元格只显示用户名,电子邮件和个人资料图像。问题是我还希望用户在用户节点中存储属于他们的UID下的属性密钥,这些用户节点将拥有在应用程序中选择的属性数组(本例中的属性将为[food,drink ,sports]),我只想显示与当前登录用户具有3个或更多相同属性的用户。



第一个问题是我不知道如何去添加一个属性到我的UID下的firebase数据库,因为它保存为索引0到索引2(3个键0,1,2)的列表的属性节点与字符串食物,饮料,运动。我也不知道这是否是最好的方法去做这件事。任何帮助将不胜感激。第二个问题是,我不知道如何获取一个新的用户,并将其添加到我的tableView IF只有该用户具有相同的属性作为当前用户。
这是我当前获取所有用户的代码,没有任何过滤

  func观察(.childAdded,如下:{(snapshot)in 

如果让词典=快照,则使用{(快照)fetchUser(){
FIRDatabase.database()。 .value as?[String:AnyObject] {
let user = User()

user.setValuesForKeys(dictionary)

self.users.append(user)


$ b DispatchQueue.main.async(执行:{
self.tableView.reloadData()
})
}

},withCancel:nil)
}

class User:NSObject {

var name:String?
var email:String?
var numberId:String?

var attributes:String?

var password:String?
var profileImageUrl:String?


解决方案

不要使用数组。他们是非常情况下,通常有其他更好的方式来存储数据。



回答第一个问题:

给定一个Firebase结构,你想要存储用户数据和一些属性,这里有一个结构会做到这一点

  users 
uid_0
name:Frank
attribs
fav_food:Pizza
fav_car:Camaro

并读取上面的节点

  usersRef.observe(。值:{快照在

(快照?.children)中的子!{
let snap = child as!FDataSnapshot
让dict = snap.value as! String:AnyObject]

let name = dict [name] //它是Frank
$ b $ at attribs = dict [attribs] as![String:AnyObject]
let food = attribs [fav_food] // pizza
let car = attribs [fav_car] // camaro
}
}

一个第二个问题:


第二个问题是,我不知道如何获取新用户,
添加它到我的tableView只有该用户具有相同的属性
当前用户。这是我目前的代码,获取所有用户,
没有任何过滤:

所以你问的是如何找到其他与当前用户具有相同属性的用户。



Firebase无法在多个子项上执行比较(即没有:where food ==比萨和汽车= Camaro)。

一个选项是将attribs存储为一个字符串

  pizza_Camaro 

然后您可以轻松检查attribs字符串是否相等。

另一种选择是递归查询。对于所有用户来说,fav_food是pizza的所有用户的查询是
$ b

 其中fav_car是Camaro 
查询从那个用户的位置....

这个想法。这不是超级有效的,但对于小数据集,它可以工作。

I have a tableView that fetches all of the users in my Firebase Database. The tableView cell simply displays the users name, email and profile image. The problem is that I also want the user to have an attributes key stored under their UID in the users node that will hold an array of attributes that they chose in the app (the attributes in this example will be ["food", "drink", "sports"]) and I want to only display the users that have 3 or more of the same attributes as the current signed in user.

The first issue is that I don't know how to go about adding an array of attributes to my firebase database under my UID as it saves as an attributes node with a list from Index 0 to Index 2 (3 keys 0,1,2) with the strings "food", "drink", "sports". I also don't know if this is the best way to go about this. Any help will be appreciated.

The second problem is that I don't know how to fetch a new user and add it to my tableView IF ONLY that user has the same attributes as the current user. Here is my current code that fetches ALL users, without any filtering:

func fetchUser() {
        FIRDatabase.database().reference().child("users").observe(.childAdded, with: { (snapshot) in

            if let dictionary = snapshot.value as? [String: AnyObject] {
                let user = User()

                user.setValuesForKeys(dictionary)

                self.users.append(user)



         DispatchQueue.main.async(execute: { 
                    self.tableView.reloadData()
                })
            }

        }, withCancel: nil)
    }

class User: NSObject {

    var name: String?
    var email: String?
    var numberId: String?

    var attributes: String?

    var password: String?
    var profileImageUrl: String?
}

解决方案

First. Don't use arrays. They are extremely situational and there are usually other, better ways to store data.

Answer to first question:

Given a Firebase structure where you want to store the user data and some attributes, here's one structure that will do it

users
  uid_0
    name: "Frank"
    attribs
      fav_food: "Pizza"
      fav_car: "Camaro"

and to read the above node

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

    for child in (snapshot?.children)! {
        let snap = child as! FDataSnapshot 
        let dict = snap.value as! [String: AnyObject] 

        let name = dict["name"] //it's Frank

        let attribs = dict["attribs"] as! [String: AnyObject]
        let food = attribs["fav_food"] //pizza
        let car = attribs["fav_car"] //camaro
    }
}

Answer to second question:

The second problem is that I don't know how to fetch a new user and add it to my tableView IF ONLY that user has the same attributes as the current user. Here is my current code that fetches ALL users, without any filtering:

So what you are asking is how to find other users that have the same attribs as the current user.

Firebase doesn't have the ability to perform comparisons upon multiple children (i.e. there is no: where food == pizza and car = Camaro).

One option is to store the attribs as a string

pizza_Camaro

then you can easily check to see if the attribs' strings are equal.

Another option is a recursive query. Here's the idea at a 10k foot level

query for all users where fav_food is pizza
   query from that set of users where fav_car is Camaro
      query from that set of users where....

You get the idea. It's not super efficient but for small datasets it can work.

这篇关于仅在符合当前用户数据的情况下才提取用户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆