gpt4 book ai didi

arrays - 仅在匹配当前用户数据时获取用户数据

转载 作者:行者123 更新时间:2023-11-28 16:01:16 24 4
gpt4 key购买 nike

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

第一个问题是我不知道如何在我的 UID 下向我的 firebase 数据库添加属性数组,因为它保存为一个属性节点,其中包含从索引 0 到索引 2 的列表(3 个键 0, 1,2) 带有字符串“food”、“drink”、“sports”。我也不知道这是否是解决此问题的最佳方法。任何帮助将不胜感激。

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

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?
}

最佳答案

首先。不要使用数组。它们是极端情况下的,通常还有其他更好的方法来存储数据。

第一个问题的答案:

给定一个 Firebase 结构,您要在其中存储用户数据和一些属性,这里有一个结构可以做到这一点

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

并读取上面的节点

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
}
}

第二个问题的答案:

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:

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

Firebase 无法对多个 child 进行比较(即没有:食物 == 披萨和汽车 = Camaro)。

一种选择是将属性存储为字符串

pizza_Camaro

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

另一种选择是递归查询。这是 10k 英尺高度的想法

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....

你明白了。它不是非常高效,但对于小型数据集它可以工作。

关于arrays - 仅在匹配当前用户数据时获取用户数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41038689/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com