【发布时间】:2017-12-10 15:59:42
【问题描述】:
我正在寻找一个函数,允许我根据我当前的朋友列表拉出更多朋友的列表。我从提取我当前的朋友列表开始,对于每个朋友,我提取他们的朋友列表并将其附加到一个数组中。但是,我现在的输出类似于 ["friendId1","friendId2"]["friendId1","friendId2","friendId3","friendId4"] 我希望它只显示第二行 ["friendId1 ","friendId2","friendId3","friendId4"]
func fetchOtherFriends(completion: @escaping ([String]) -> Void) {
let currentUser = Auth.auth().currentUser!.uid
Database.database().reference().child("friends").child(currentUser).observeSingleEvent(of: .value, with: { (snapshot : DataSnapshot) in
// Iterate through all the children:
for child in snapshot.children.allObjects as! [DataSnapshot] {
let value = child.value as? NSDictionary
let userId = value?["userId"] as? String ?? ""
Database.database().reference().child("friends").child(userId).observeSingleEvent(of: .value, with: { (snapshot : DataSnapshot) in
// Iterate through all the children:
for child1 in snapshot.children.allObjects as! [DataSnapshot] {
let value1 = child1.value as? NSDictionary
let userId1 = value1?["userId"] as? String ?? ""
self.otherfriendsArray.append(userId1)
completion(self.otherfriendsArray)
}
})
}
})
}
【问题讨论】: