问题
The title maybe looks wrong, but its what I am trying to figure out how to do this. My data structure looks like this:
I want to look if there are users presented in every channel. This will work to check if there are channels:
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 {
}
})
This will of course check if there is a child with a property called name, and if so, do stuff. What I want to do, is the loop through the children (users in a channel) in the children(a channels) in all channels. I want to check if the channel has any children's in it, if not, remove that channel. So how to loop through the children in the channel children? I have access to every channels ID by calling this function:
let id = (channelSnap as! FIRDataSnapshot).key
Edit:
This prints out No user in channel:
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")
}
}
Edit 2:
This is a print of snapshot, followed by a print of channelSnap, while there is a user in it:
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 = "";
}
回答1:
Is the KeJTqVREbMEmtD0oAk4 a user inside the channel? If so I think it would be nice to have a key users where you would keep all the informations about them...
In the image below, The example key would correspond to your channel. Inside it, you'd have other_info, which would be the informations about that channel. Then, there's users, which will keep all the different users that are in the channel.
Then channelSnap.child("users").value would have all the users in that channel.
You could check whether there are users with something like:
if let dict = channelSnap.child("users").value as? [String: Any] {
if dict.count == 0 {
// no users
} else {
// you got users
}
}
回答2:
If you create a key for players will be easy check if have the player and retrieve the data.
To catch the data will be like this:
ref.child("channels").observeSingleEvent(of: .childAdded, with: { (snapshot) in
guard let data = snapshot.value as! [String:AnyObject]? else { return print("Snapshot error!")}
if let player = data["players"] {
for player in player as! [String:[String:AnyObject]] {
print(player.key)
print(player.value as Any)
}
} else {
print("No player found!")
}
})
来源:https://stackoverflow.com/questions/42599617/how-to-check-for-data-in-a-child-in-a-child-in-firebase