使用Firebase将多个循环中的值存储到数组中。迅速

时间:2018-01-28 18:43:57

标签: ios firebase firebase-realtime-database

我一直在试图弄清楚如何获取一组用户数据,并将其与其他用户数组列表进行比较,以确定它是否与它们匹配。我查看了答案here,它表明我应该构建我的数据,以便它非规范化,因此有重复。这种方法有效但我现在正在努力将这些数组存储到一个数组中,以后可以使用它。

以下是我的数据结构示例:

{
  “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
      ]
   }
  }
}

我可以查询杰克所拥有的兴趣。然而,循环发生两次,因此我很难附加一个数组来存储杰克正在寻找的用户的值。

这是我查询技能的方式:

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

这是我在控制台上打印时的数组:

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

当我期待类似的东西时:

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

更新

回应下面的评论,arrayOfInterestKeys是从interestList获取的密钥,我在查询我想要观察的技能时将其用作参考,以下是我如何获取它们:

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

1 个答案:

答案 0 :(得分:0)

你有:

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()