gpt4 book ai didi

ios - Swift Firebase 从多个节点读取

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:31:11 25 4
gpt4 key购买 nike

我正在尝试解决以下问题:我想使用跨两个节点的匹配信息填充 Collection View 单元格,并且需要在“播放器”节点中多次读取。

这是我的 Firebase 数据库结构

{
Players:
LpWgezRkC6EWS0sjXEWxhFl2: {
userName: 'John Doe'
teamId: '234'
teamName: 'Revenge'
teamLogo: 'star.png'
etc...
},
RfskjEWSkdsjkjdskjsd12fg: {
userName: 'Jane Doe'
teamId: '987'
teamName: 'Frills'
teamLogo: 'jag.png'
etc...
}
},
Matches:
12345: {
User1: 'LpWgezRkC6EWS0sjXEWxhFl2'
User2: 'RfskjEWSkdsjkjdskjsd12fg'
date: '11/10/17'
WeekId: 19
etc...
}
}
}

如您所见,“Matches”节点包含“Players”信息,因此在 Collection View 中我希望显示 player1 与 player2 的信息。

我目前的代码是这样的:

self.ref.queryOrdered(byChild: "WeekId").queryEqual(toValue: 19).observe(.value, with: { snapshot in 

var items: [Match] = []

for item in snapshot.children {

let snapshotValue = (item as! DataSnapshot).value as? NSDictionary

let pId1 = snapshotValue!["User1"] as! NSString
let pId2 = snapshotValue!["User2"] as! NSString

let match = Match(snapshot: item as! DataSnapshot)

items.append(match)

}

self.matches = items

self.collectionView?.reloadData()
}

我不太确定如何对“玩家”节点进行第二次查找(我需要 2 个),因为它需要查找两个玩家的信息,所有这些都不会超过 let match = Match(snapshot : item as!DataSnapshot) 函数,否则会失败?

谁能帮帮我!

最佳答案

可以添加

self.ref.queryOrdered(byChild: "WeekId").queryEqual(toValue: 19).observe(.value, with: { snapshot in

var items: [Match] = []

for item in snapshot.children {

let snapshotValue = (item as! DataSnapshot).value as? NSDictionary

let pId1 = snapshotValue!["User1"] as! NSString
let pId2 = snapshotValue!["User2"] as! NSString

fetchUserProfile(withUID: pId1, completion: { (userDict1) in
// Here you get the userDict 1
self.fetchUserProfile(withUID: pId2, completion: { (userDict2) in
//Here you get the user dict 2
let match = Match(snapshot: item as! DataSnapshot)
items.append(match)
})
})
}

self.matches = items

self.collectionView?.reloadData()
})

//完成获取用户资料

func fetchUserProfile(withUID uid: String, completion: @escaping (_ profileDict: [String: Any]) -> Void) {
// New code
Database.database().reference().child(uid).observe(.value, with: { snapshot in
// Here you can get the snapshot of user1
guard let snapDict = snapshot.value as? [String: Any] else {return}
completion(snapDict)
})
}

我认为这不是解决此问题的正确方法。我建议您捕获所有用户的 pID 并将其保存在一组 UserProfiles 中。需要时,您可以从该数组中获取用户配置文件。希望对您有所帮助。

关于ios - Swift Firebase 从多个节点读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46991228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com