使用Firebase将值与值数组进行比较.迅速 [英] Compare values with an array of values with Firebase. Swift

查看:86
本文介绍了使用Firebase将值与值数组进行比较.迅速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要与值数组进行比较的值.基本上,一个用户需要检查它是否与另一个用户具有相同的项目.但是我正在努力解决这个问题,因为我如何获得一系列用户的商品?通常,当您观察一个值时,您会执行以下操作:

I have a value which needs to be compared with an array of values. Basically a user needs to check if it has the same item as another user. But I am struggling to solve this as how do I get the item of an array of users? Normally when you observe a value you do something like this:

Database.database().reference(withPath: "users/\(userID)/itemList").observeSingleEvent...

但是,当涉及到一个用户数组itemList时,如果有多个ID,该如何实现?我想将一个或多个用户项与其他用户项进行比较,并检查它们是否匹配,以便对具有匹配项的用户数组进行排序.

However, when it comes to an array of users itemList how can this be achieved if there are multiple ID's? I'd like compare a user item or items with other users item and check if they match in order to sort an array of users that have a match.

如果已经有一个示例,请帮助指导我.

If there is already an example for this please help direct me there.

更新

这是我的数据结构的样子:

This is how my data structure looks:

{
  "users": {
    "EWJGFYmVTzOiHgnq42ebhrg2fj": {
      "firstName": "Friederike",
      "itemList": [
        2: true,
        3: true,
        0: true
      ]
    },

    "C076OYmVTzOiHgnq4wPQtY2XpED2": {
      "firstName": "Ian",
      "itemList": [
        0: true,
        1: true,
        3: true
      ]
    },
    "Juoiuf0N6qNmkm32jrtu6X6UK62": {
      "itemList": [
        0: true
      ],
      "firstName": "Jack"
    }
  }
}

更新2.0

使用下面的答案,我能够查询项目表,但仍无法查询匹配的键,因为可能存在多个数组,因此我无法使用它进行过滤或其他操作.

With the answer below I am able to query through the items table but still unable to query the keys that match as there can be multiple arrays and therefore I cannot use it to filter or anything.

推荐答案

好的,因此对于您当前的数据结构,您必须查询用户下的所有节点,然后比较数组,这是非常低效的.没有修改结构就没有直接或简单的方法.因此,我建议您修改数据结构,使每个项目都有一个拥有它的所有用户的列表.像这样:

Ok so with your current data structure you'd have to query all nodes under users and then compare the arrays, which is very inefficient. There isn't a straight forward or easy way to do that without modifying your structure. So I suggest you modify your data structure so that each item has a list of all users that have it. Something like this:

{
  "items": {
    "0": {
      "EWJGFYmVTzOiHgnq42ebhrg2fj": "Friederike",
      "C076OYmVTzOiHgnq4wPQtY2XpED2": "Ian",
      "Juoiuf0N6qNmkm32jrtu6X6UK62": "Jack"
    },

    "1": {
      "C076OYmVTzOiHgnq4wPQtY2XpED2": "Ian"
    },

    "2": {
      "EWJGFYmVTzOiHgnq42ebhrg2fj": "Friederike"
    }
    //....
  }
}

取决于您要显示的内容,您可能希望存储的信息不仅仅是用户的UID和用户名.您可以使用如下查询查询与您具有相同项目的所有用户:

Depending on what you want to display you might want to store more information than just the users UID and username. You can query all the users that have the same items as you using a query like this:

let ref = Database.database().reference()
// assuming you already have the current users items stored in an array
for item in items {
    ref.child("items").child(String(item)).observeSingleEvent(of: .value, with: { snap in
        for child in snap.children {
            let child = child as? DataSnapshot
            if let key = child?.key, let name = child?.value as? String {
                // do something with this data
            }
        }
    })
}

Firebase数据库为noSQL,因此数据应为嵌套数据.查看此问题以了解更多信息.

Firebase database is noSQL, so data is meant to be denormalized or duplicated so that queries can be optimized. Firebase actually recommends that you avoid nesting data. Take a look at this question for more information.

希望有帮助

与评论中提出的问题相关的代码

Code related to question asked in comments

假设您将具有相同项目的UID或用户名存储在字符串数组中,则可以

Assuming you are storing the UID's or names of users with the same items in a string array you can prevent duplicates using .contains()

var namesWithMatchingItems = [String]()

if !namesWithMatchingItems.contains(nameYouJustFetched) {
    namesWithMatchingItems.append(nameYouJustFetched)
}

这篇关于使用Firebase将值与值数组进行比较.迅速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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