首页 文章

根据BOOL状态交朋友,发现难以比较数组值,Firebase,Swift

提问于
浏览
0

我正在制作一个聊天应用程序,我只想在这样的条件下交朋友,如果 userA 有朋友 userB == tureuserBuserA == true ,我希望他们交朋友 . 为了让你更清楚这里是 json 格式:

user_friends: {
$userA: {
  friends: {
    $userB: true
  }
},
$userB: {
  friends: {
    $userA: true,
    $userC: true
  }
},
$userC: : {
  friends: {
    $userA: true,
    $userB: true
  }
 }
}

如上面的 jsonuserAuserB 作为朋友, userBuserAuserC 作为朋友, userC 将只有 userB 作为朋友因为 userA 没有 userC 作为他的朋友 . 这是我的数据模型,不确定它是否完美

import Foundation
import SwiftyJSON

struct Friends {
var myFriends:[Bool]!

struct Friend {
let key: String
let is_friend: Bool
init(snap: FIRDataSnapshot) {
    self.key = snap.key
    self.is_friend = (snap.value != nil)
   }
}

这是我试图获取数据并比较它们的功能,但它不能按我的意愿工作

func getFriendList() {

    if self.user != nil {
            _ = self.ref.child("user_friends").child((user?.uid)!).observeSingleEvent(of: .value, with: { snapshot in
                for snap in snapshot.children {
                  for friends in (snap as AnyObject).children {
                    // cast friend and append to myFriends
                    print(friends)
                    let friend = Friend(snap: friends as! FIRDataSnapshot)
                    self.myFriends.append(friend)
                    print("my friends are\(self.myFriends.description)")
                    print("friends count\(self.myFriends.count)")

                    if (self.myFriends.count > 0) {
                        for friend in self.myFriends {
                            _ = self.ref
                                .child("user_friends")
                                .child(friend.key)
                                .child("friends")
                                //.child((self.user?.uid)!)
                                .observeSingleEvent(of: .value, with: { snapshot in
                                print("Desired Snapshot\(snapshot)")
                                //Now I need to check the condition here

                          })
                        }
                      }
                    }
                }
            })


    }//FIRAuth ends here

}//getFriendList ends here

这是我的实际数据库设计:
friends
和调试结果
enter image description here
我想我需要拿两个字典并比较它们的值 . 它几乎像 instagram 功能,如果有人跟踪用户并且用户跟随他,他们启用了消息选项,消息选项不会打开,直到他们互相跟随 . 请任何善良的人出面帮助我 . 我遇到了麻烦 . 谢谢 .

1 回答

  • 1

    你正在做很多你不需要的循环和解析 . 这就是我要做你想做的事情:

    // A struct for friend nodes on a user account
    struct Friend {
        let key: String
        let is_friend: Bool
        init(snap: FIRDataSnapshot) {
            self.key = snap.key
            self.is_friend = snap.value as! Bool
        }
    }
    // A struct for user account nodes
    struct ChatUser {
        var friends = [Friend]()
        let user_ref: FIRDatabaseReference
        let key: String
        init(user_ref: FIRDatabaseReference) {
            self.user_ref = user_ref
            self.key = user_ref.key
        }
    }
    
    let currentUser = ChatUser(user_ref: self.user.ref)
    
    func getFriendListForUser(user: ChatUser) {
        _ = user.ref.child("user_friends").observeSingleEvent(of: .value, with: { snapshot in
            for friend in snapshot.children {
                // cast friend and append to myFriends
                let friend = Friend(snap: friend)
                user.friends.append(friend)
            }
        })
    }
    
    // validate friend list in firebase for a given chat user
    func validatedFriendsForUser(user: ChatUser) {
        for friend in user.friends {
            _ = FIRDatabase.database()
                .reference(withPath: friend.key)
                .child("friends")
                .child(self.currentUser.key)
                .observeSingleEvent(of: .value, with: { snapshot in
                    if let status = snapshot as? Bool {
                        friend.is_friend = status
                    }
                })
        }
    
        // Remove friends which are not confirmed in firebase
        self.currentUser.friends = user.friends.filter{ $0 }
    }
    // call our methods to fetch and filter user friends for current user
    self.getFriendListForUser(user: self.currentUser)
    self.validatedFriendsForUser(user: self.currentUser)
    

    validatedFriendsForUser()不是必需的,但是如果要检查用户A是否在数据库中将用户B的密钥设置为 true .

相关问题