在Swift 3中追加到Firebase闭包内的数组 [英] Append to array inside a Firebase Closure in Swift 3

查看:59
本文介绍了在Swift 3中追加到Firebase闭包内的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试附加到Firebase闭包内的数组, 我在闭包var CanJoinArray = [String]()外部声明了数组,但是当我尝试在闭包内部添加数组:self.CanJoinArray.append("hello")并在闭包外部打印时,print (CanJoinArray)不打印.但是,当我在(内部)闭包中打印数组时,它可以追加并打印.我该如何解决?谢谢!

I am trying to append to an array inside a Firebase closure, I declared the array outside of the closure var CanJoinArray = [String]() But when I try to append to the array inside the closure: self.CanJoinArray.append("hello") and print it outside of the closure, print (CanJoinArray) it doesn't print. However, when I print the array inside the (inside) closure, it is able to append and print. How can I solve this? Thanks!

ref.observeSingleEvent(of: .value, with: { (FIRDataSnap) in
        for child in FIRDataSnap.children.allObjects {
            let key = (child as AnyObject).key as String
            self.myArray.append(key)
        }

        for (_, element) in self.myArray.enumerated() {
            self.ref.child(element).child("Players").observeSingleEvent(of: .value, with: { (Snap) in
                if Snap.childrenCount < 2 {
                    self.CanJoinArray.append("hello")
                }
                else {
                    print("Can't join lobby\(element)... Full!")
                }
                print(CanJoinArray) //this does work
            })
        }
       print (CanJoinArray) //this doesn't work
    })
}

数据库结构:

{
 "Lobbies" : {
    "RANDOMUUID" : {
       "LobbyName" : {
          "LobbyName" : ""
       },
       "Players" : {
          "user1" : "USERUID#"
       }
    }
 },
 "Users" : {
    "USERUID#" : {
      "DisplayName" : "user1"
    }
  }
}

推荐答案

您可以选择以下两个方向...

There are a couple of directions you can go...

您似乎想要维护一个可用大厅的列表,而可用性取决于大厅中玩家的数量.在这种情况下,0或1个玩家表示该游戏可用,如果2个或更多玩家表示该游戏已满.

It appears that you want to maintain a list of lobbies that are available, and the availability is determined by the number of players in the lobby. In this case 0 or 1 players means it's available and if 2 or more means it's full.

想到的第一个选择是通过在大厅内添加一个is_available节点来更改您的结构.当玩家加入该大厅时,将其uid添加到玩家子代,如果is_available是第二位玩家,则将is_available更新为false.结构看起来像这样:

The first option that comes to mind is to change your structure by adding a is_available node within the lobby. When a player joins that lobby, add their uid to the players child and update the is_available to false if it's the second player. The structure would look something like this:

Lobbies
  lobby_id_0
    lobby_name: "lobby 5"
    is_available: false
    players
      uid_0: true
      uid_1: true
  lobby_id_1
    lobby_name: "lobby 12"
    is_available: true
    players
      uid_2: true
  lobby_id_2
    lobby_name: "some lobby"
    is_available: false
    players
      uid_3: true
      uid_4: true

该代码将是一个查询:

let lobbiesRef = rootRef.child("Lobbies")
let queryRef = lobbiesRef.queryOrdered(byChild: "is_available").queryEqual(toValue: true)
queryRef.observeSingleEvent(of: .value, with: { (snapshot) in

    for child in snapshot.children {
       let snap = child as! FIRDataSnapshot
       let lobbyDict = snap.value as! [String: Any]
       let lobbyKey = snap.key
       self.availableLobbyArray.append(lobbyKey)
    }
})

这将在数组中添加lobby_id_1,因为其他两个大厅每个都有两个玩家.

This would add lobby_id_1 to the array as the other two lobbies each have two players.

第二种解决方案是保留第二个列出可用大厅的节点.

A second solution is to keep second node that lists lobbies that are available.

这是All_Lobbies节点和Available_Lobbies节点

Here's the All_Lobbies node and the Available_Lobbies node

All_Lobbies
   lobby_id_0
     lobby_name: "lobby 5"
     players
         uid_0: true
         uid_1: true
   lobby_id_1
     lobby_name: "lobby 12"
     players
        uid_2: true

Available_Lobbies
   lobby_id_1: true

同样的概念;当一个玩家加入lobby_id_1并且是两个玩家(因此已满)时,只需从"Available_Lobbies"节点中删除该大厅即可.

Kind of the same concept; when a player joins lobby_id_1 and it's two players (so it's full), simply remove that lobby from the Available_Lobbies node.

查看可用的代码:

let availableLobbiesRef = rootRef.child("Available_Lobbies")
availableLobbiesRef.observeSingleEvent(of: .value, with: { (snapshot) in

    for child in snapshot.children {
       let snap = child as! FIRDataSnapshot
       let lobbyKey = snap.key
       self.availableLobbyArray.append(lobbyKey)
    }
})

这种结构的优点是双重的.我们完全消除了查询,因为只有可用的大厅存储在该节点中.查询要比观察值重"得多,并且会占用更多资源.第二件事是,如果有1百万个Available_Lobbies,则我们正在加载的数据要少得多!

The advantage of this structure is two-fold. We eliminate the query completely because only available lobbies are stored in that node. Queries are much 'heavier' than an observe and take more resources. The second thing is that if there are 1 Million Available_Lobbies, we are loading in far less data!

这篇关于在Swift 3中追加到Firebase闭包内的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆