计算Firebase中的共同朋友数 [英] Count number of mutual friends in Firebase

查看:79
本文介绍了计算Firebase中的共同朋友数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用任何给定的userID来增加我的共同朋友的数量.我首先从Firebase中拉出了所有朋友,然后又拉出了userID的朋友.我只是不确定如何从那里计算共同朋友的数量.如果我做错了方法,请告诉我.

I'm trying to pull the number of mutual friends that I have with any given userID. I first pulled all my friends from Firebase and then I pulled the userID's friends. I'm just not sure how to count the number of mutual friends from there. Please let me know if I'm going about it the wrong way.

func fetchMutualFriends(myID: String, userID: String) {

    let myID = Auth.auth().currentUser!.uid

    let postRef = self.databaseRef.child("friends").child(myID)

    postRef.observe(.value, with: { (snapshot) in
        for childSnapshot in snapshot.children {
              print(childSnapshot)

        }
    })
    let postRef1 = self.databaseRef.child("friends").child(friendID)

    postRef1.observe(.value, with: { (snapshot) in
        for childSnapshot in snapshot.children {
            print(childSnapshot)

        }
    })
}

Firebase结构

Firebase structure

root/
|___ friends/
|      |___ myID
|              |___ friendID1
|      |___ friendID1
|              |___ myID

推荐答案

由于实际结构尚不清楚,因此我们假设这样的标准结构

Since the actual struture is not clear, let's assume a standard structure like this

  "users"
    "user_0"
      "friends"
        "user_1" : true,
        "user_2" : true,
        "user_3" : true
    "user_1" 
      "friends"
        "user_0" : true,
        "user_2" : true
    "user_2"
      "friends"
        "user_0" : true,
        "user_1" : true,
        "user_3" : true
    "user_3"
      "friends"
        "user_0" : true,
        "user_2" : true

如您所见,用户0是与user_1,user_2和user_3的朋友,同样,在它们的节点中,这些用户也是与user_0的朋友.

As you can see, user 0 is friends with user_1, user_2 and user_3, and likewise in their nodes those users are friends with user_0.

如果我们想保留这种结构,那么找到两个用户共同的朋友的交集就很容易了.

If we want to keep this structure then finding the intersection of two users mutual friends is pretty easy.

假设我们要查找user_0和user_2共有哪些朋友.查看结构,我们发现它是

Suppose we want to find which friends user_0 and user_2 have in common. Looking at the structure we find it's

user_1
user_3

我们如何在代码中做到这一点?

How do we do that in code?

let user1FriendsRef = self.ref.child("users").child("user_0").child("friends")
let user2FriendsRef = self.ref.child("users").child("user_2").child("friends")

user1FriendsRef.observeSingleEvent(of: .value, with: { user1Snapshot in
    let user1FriendsArray = user1Snapshot.children.map { ($0 as! DataSnapshot).key }
    let user1FriendsSet = Set(user1FriendsArray)

    user2FriendsRef.observeSingleEvent(of: .value, with: { user2Snapshot in
        let user2FriendsArray = user2Snapshot.children.map { ($0 as! DataSnapshot).key }
        let user2FriendsSet = Set(user2FriendsArray)

        let mutualFriends = user1FriendsSet.intersection(user2FriendsSet).sorted()
        print(mutualFriends)
        print(mutualFriends.count)
    })
})

代码的工作方式如下:我们首先引用要比较的两个用户朋友列表

Here's how that code works: We start with a reference to the two users friends lists we want to compare

使用observeSingleEvent,将读取第一个用户朋友列表,并将键(user_1,user_2等)存储在数组中.完成我的映射(迭代)每个键:值对以捕获键.该数组将转换为Set var.

Using observeSingleEvent, the first users friends list is read and the keys (user_1, user_2 etc) are stored in an array. That is done my mapping (iterating over) each key: value pair to capture the key. That array is converted to a Set var.

我们对第二个用户执行相同的操作-请注意,在第一个闭包内执行此操作很重要,因此我们可以确保第一个用户的结果是完整的.将这些结果映射到数组,然后将其制成Set.

We do the same thing for the second user - noting it's important we do that within the first closure so we are guaranteed the results from the first user are complete. Those results are mapped to an array and the made into a Set.

然后,利用Set的交集功能然后打印共同的朋友(已排序)的魔力

Then, the magic of leveraging the intersection function of a Set and then print the mutual friends (sorted)

["user_1", "user_3"]
2

这篇关于计算Firebase中的共同朋友数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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