2017-03-04 215 views
2

標題可能看起來不正確,但它試圖弄清楚如何做到這一點。我的數據結構是這樣的:如何檢查兒童中的數據,在Firebase中的兒童

data structure

我想看看是否有每個通道呈現用戶。這將工作,以檢查是否有渠道:

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 = ""; 
} 

回答

1

users in channel?

是在KeJTqVREbMEmtD0oAk4通道內用戶?如果是這樣,我認爲這將是很好,有一個密鑰users,你會保留所有關於他們的信息...

在下面的圖片中,example鍵將對應您的頻道。在裏面,你會有other_info,這將是關於該頻道的信息。然後,有users,這將保留通道中的所有不同用戶。

enter image description here

然後channelSnap.child("users").value將在該頻道中的所有用戶。

你可以檢查是否有用戶喜歡的東西:

if let dict = channelSnap.child("users").value as? [String: Any] { 
    if dict.count == 0 { 
     // no users 
    } else { 
     // you got users 
    } 
} 
+0

是,KeJTqVREbMEmtD0oAk4是一個隨機的用戶。這可以工作,但我想以我的方式來做,其他我得到越來越多的地圖。是否有可能在頻道中搜索名爲「userID」的鍵,因爲該頻道中的每個用戶都應該有一個?如果用戶標識沒有密鑰,則不存在用戶。我想聽聽這是否可能! – Petravd1994

+0

是的..你可以,但問題是'userId'是在一個radom id裏面,所以你必須到處搜索它。除非你在某個地方有對該密鑰的引用。 –

+0

但是,如果你想這樣做,你必須在channelSnap.children中添加另一個'for' .. '''用於shapshot.children中的channelSnap for {0} {0} {0}孩子在channelSnap.children { //在此處查找userId } } ''' –

1

如果你創建了玩家關鍵將是很容易檢查,如果有玩家和檢索數據。 Will look 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!") 
     } 
    })