標題可能看起來不正確,但它試圖弄清楚如何做到這一點。我的數據結構是這樣的:如何檢查兒童中的數據,在Firebase中的兒童
我想看看是否有每個通道呈現用戶。這將工作,以檢查是否有渠道:
channelRef.observeSingleEvent(of: .value, with: { (snapshot) -> Void in
for channelSnap in snapshot.children {
let channelData = (channelSnap as! FIRDataSnapshot).value as! Dictionary<String, AnyObject>
if let name = channelData["name"] as! String!, name.characters.count > 0 {
}
})
檢查當然這會在出現與屬性名爲名稱的孩子,如果是的話,做的東西。我想要做的是通過所有頻道中兒童(頻道)中的兒童(頻道中的用戶)循環播放。我想檢查頻道是否有任何兒童,如果沒有,請移除該頻道。那麼如何循環播放頻道中的孩子呢?我有機會到每一個渠道ID通過調用這個函數:
設ID =(!channelSnap爲FIRDataSnapshot).KEY
編輯:
這種打印出的通道中沒有用戶:
channelRef.observeSingleEvent(of: .value, with: { (snapshot) -> Void in
for channelSnap in snapshot.children {
for child in (channelSnap as AnyObject).children {
let usersData = (child as! FIRDataSnapshot).value as? Dictionary<String, AnyObject>
if let randomUser = usersData?["userID"] as! String!{
print("user in the channel")
}
else
{
print("no user in channel")
}
}
編輯2:
這是快照打印,其後是打印的channelSnap,,同時有一個用戶在裏面:
SNAPSHOT: Snap (channels) {
"-KePh6YFmQqQ6ZhEfTHn" = {
"-KePh6YHPLSAIEARfj-i" = {
PictureVersion = 2;
readyToGo = 0;
userID = SZlQ76RLCJQpFa0CDhrgFJoYzrs2;
username = pietje;
};
creator = SZlQ76RLCJQpFa0CDhrgFJoYzrs2;
currentPlayers = 1;
entryFee = 100;
gameType = normal;
maximumPlayers = 4;
name = "random channel";
password = "";
};
}
CHANNELSNAP: Snap (-KePh6YFmQqQ6ZhEfTHn) {
"-KePh6YHPLSAIEARfj-i" = {
PictureVersion = 2;
readyToGo = 0;
userID = SZlQ76RLCJQpFa0CDhrgFJoYzrs2;
username = pietje;
};
creator = SZlQ76RLCJQpFa0CDhrgFJoYzrs2;
currentPlayers = 1;
entryFee = 100;
gameType = normal;
maximumPlayers = 4;
name = "random channel";
password = "";
}
是,KeJTqVREbMEmtD0oAk4是一個隨機的用戶。這可以工作,但我想以我的方式來做,其他我得到越來越多的地圖。是否有可能在頻道中搜索名爲「userID」的鍵,因爲該頻道中的每個用戶都應該有一個?如果用戶標識沒有密鑰,則不存在用戶。我想聽聽這是否可能! – Petravd1994
是的..你可以,但問題是'userId'是在一個radom id裏面,所以你必須到處搜索它。除非你在某個地方有對該密鑰的引用。 –
但是,如果你想這樣做,你必須在channelSnap.children中添加另一個'for' .. '''用於shapshot.children中的channelSnap for {0} {0} {0}孩子在channelSnap.children { //在此處查找userId } } ''' –