使用Firebase将来自多个循环的值存储到数组中.迅速 [英] Store values from multiple loops into an array with Firebase. Swift

查看:64
本文介绍了使用Firebase将来自多个循环的值存储到数组中.迅速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试找出如何获取用户数据数组,并将其与其他用户数组列表进行比较,以查看其是否与它们匹配.我已经在此处中查看了答案,该答案表明我应该对数据进行结构化,以便对其进行非规范化,因此有重复项.这种方法行得通,但我现在正努力将这些数组存储到一个数组中,以便以后使用.

I've been trying to figure out how to get an array of users data and compare it with a list of other users array to see if it matches them. I've looked at the answer here which demonstrates that I should structure my data so that it is denormalised and therefore have duplicates. This approach works but I am now struggling to store these array into one array which can be used to later on.

这是我的数据结构的一个示例:

Here is an example of my data structure:

{
  "skills": [
    {
      "name": "Walking"
    },
    {
      "name": "Running",
      "usersWithSkill": [
        "user1": true,
        "user2": true,
        "user3": true
      ]
    },
    {
      "name": "Swimming",
      "usersWithSkill": [
        "user3": true
      ]
    },
    {
      "name": "Dancing",
      "usersWithSkill": [
        "user1": true,
        "user3": true
      ]
    }
  ],
  "users": {
    "user1": {
      "firstName": "Amelia",
      "skillsList": [
        "1": true,
        "3": true
      ]
    },
    "user2": {
      "firstName": "Elena",
      "skillsList": [
        "1": true,
      ]
    },
    "user3": {
      "firstName": "John",
      "skillsList": [
        "1": true,
        "2": true,
        "3": true
      ]
    },
    "user4": {
      "firstName": "Jack",
      "interestsList": [
        "1": true,
        "3": true
      ]
   }
  }
}

我能够查询 Jack 的兴趣.但是该循环发生了两次,因此我很难添加一个存储 Jack 正在寻找的用户值的数组.

I am able to query the interests which Jack has. However the loop happens twice and therefore I'm struggling to append an array that stores the values of the users that Jack is looking for.

这是我查询技能的方式:

This is how I query the skills:

var newArray = [String]()

func fetchSkillKeys() {

    // Loop through the array of interest keys
    for key in self.arrayOfInterestKeys {
        let interestRef = Database.database().reference(withPath: "skills/\(key)/usersSkill")
        interestRef.observeSingleEvent(of: DataEventType.value, with: { (snapshot) in
            for child in snapshot.children {
                guard let snapshot = child as? DataSnapshot else { continue }
                self.newArray.append(snapshot.key)
            }
            print(self.newArray)
        })
    }
}

这是我的阵列在控制台上打印时的样子:

This is how my array when printed on the console:

[]
["0", "1", "2"]
["0", "1", "2", "0", "1", "2"]
["0", "1", "2", "0", "1", "2", "0", "1"]

当我期待类似的东西时:

When I'm expecting something like:

["0", "1", "2", "0", "1"]

更新

响应下面的评论,arrayOfInterestKeys是从interestsList中获取的键,当我查询要观察的技能时,我将其用作参考,这是我如何获取它们的方法:

Responding to the comment below, the arrayOfInterestKeys is keys taken from the interestsList which I use as a reference when querying the skills I want to observe, here is how I fetch them:

var arrayOfInterestKeys = [String]()

func fetchUserInterests() {

    guard let uid = Auth.auth().currentUser?.uid else { return }
    // Go to users interest list
    let databaseRef = Database.database().reference(withPath: "users/\(uid)/interestsList")

    // Observe the values
    databaseRef.observeSingleEvent(of: DataEventType.value, with: { (snapshot) in

        // Loop through the interests list
        for child in snapshot.children {
            guard let snapshot = child as? DataSnapshot else { continue }
            // Populate an empty array
            self.arrayOfInterestKeys.append(snapshot.key)
        }
   })
}

推荐答案

您有:

for child in snapshot.children {
    guard let snapshot = child as? DataSnapshot else { continue }
    self.newArray.append(snapshot.key)
}
print(self.newArray)

尝试:

for child in snapshot.children {
    let child = child as? DataSnapshot
    if let key = child?.key {
        // Make sure the key is not in the array
        if self.newArray.index(of: key) == nil {
            self.newArray.append(key)                                
        }
    }
}

我也想知道您如何呼叫fetchSkillKeys()

Also I'd like to know how / where you are calling fetchSkillKeys()

这篇关于使用Firebase将来自多个循环的值存储到数组中.迅速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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