无法在Firestore内部更新的变量获取查询

时间:2019-06-19 16:47:55

标签: swift firebase google-cloud-firestore

主要问题:消息未更改为“已创建房间!”

运行时,返回的字符串消息应更改。但是,它没有,查询功能仍在运行并设置信息,因此我知道它应该在编辑消息以返回。

newRoomId是完全随机的6个不重叠的字符串,因此,在调试过程中,除非有十亿分之一的机会,否则绝对不要将true设为true。

我尝试将邮件保存到用户数据,但这也不起作用。

func createRoom() -> String{ 
var message = "Error"
let userDefault = UserDefaults.standard
var taken = false
var roomsOwned = [String]() //Gets modified in another part of this function that is not included here
db.collection("chatRooms").document(newRoomId).getDocument{(document, error) in
                if(document!.exists){
                    taken = true
                }
                else{
                    self.db.collection("chatRooms").document(newRoomId).setData([
                        "Owner" : self.userID,
                        "Name" : String(self.username + "'s Room"),
                        "Members" : [self.userID]
                        ])
                    roomsOwned.append(newRoomId)
                    self.userDefault.set(roomsOwned, forKey: "OwnedRooms") // This array is created in a previous query of this function, but it uploads properly because I can see it in the database.
                    self.db.collection("users").document(self.userID).setData(["OwnedRooms" : roomsOwned], merge: true)
                    message = "Room created!"
                }
                if(taken){
                    message = self.createRoom()
                }
            }
return message // //RETURNS "Error" (And then I check the database, and see it's updated)
}

输出为“错误”(或任何我设置top变量为的值) 我希望“创建房间!” 另外,如果我让它返回roomsOwned.count,它将为0。但是self.UserDefault.array(forKey:“ OwnedRooms”)。count将为12,并且每次函数运行时都会增长,这是我期望的

0 个答案:

没有答案