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

时间:2017-03-11 06:24:31

标签: swift firebase swift3

我正在制作聊天应用,我想只有在 userA 有朋友 userB == ture 并且 userB 具有 userA == true ,我希望他们成为朋友。为了让您更清楚,这里是 json 格式:

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

如上面的 json userA userB 作为朋友, userB userA & userC 作为朋友, 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 个答案:

答案 0 :(得分: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