我在对应用程序中的某些代码进行故障排除时遇到困难。总而言之,我相信我能够成功地查询特定集合中的文档,但是它没有填充我的数组。我试图重新构建整个代码以进行故障排除,但是我很茫然。我有一个类似的视图控制器,它使用类似的代码,并且运行良好,这就是为什么我在弄清楚这一点时会遇到麻烦,并且这让我很生气。
我很想提出一些有关如何解决问题的想法。并提前感谢您!!
两张图片 1.屏幕截图显示了我的打印语句的控制台结果 2.我的Firestore集合的屏幕截图
函数中的代码
var gamesArray = [Games]()
....
private func loadGames() {
SVProgressHUD.show()
let collectionReference =
db.collection("games").whereField("playerid", isEqualTo: "30019")
collectionReference.addSnapshotListener { documentSnapshot, error in
if (documentSnapshot?.isEmpty)! {
print("Oops: No Data found in query")
SVProgressHUD.dismiss()
} else {
guard let documents = documentSnapshot?.documents else {
return
}
//2. This is part of the code is firing
//But my Array is not populating with any data (See Screen
Shot)
print("Success: Query Data Found")
self.gamesArray = documents.compactMap({Games(dictionary:
$0.data())})
print(self.gamesArray)
self.reloadMyPlayers()
}
}
}
func reloadMyPlayers() {
DispatchQueue.main.async {
self.gameView.reloadData()
}
SVProgressHUD.dismiss()
}
这是我的游戏结构
import Foundation
import FirebaseFirestore
protocol DocSerializable {
init?(dictionary:[String : Any])
}
struct Games {
var playerid:String
var opponent:String
var season:String
var gamedate:Timestamp
var submitdate:Timestamp
var dictionary:[String : Any] {
return [
"playerid" : playerid,
"opponent" : opponent,
"season" : season,
"gamedate" : gamedate,
"submitdate" : submitdate
]
}
}
extension Games : DocSerializable {
init?(dictionary: [String : Any]) {
guard let playerid = dictionary["playerid"] as? String,
let opponent = dictionary["opponent"] as? String,
let season = dictionary["season"] as? String,
let gamedate = dictionary["gamedate"] as? Timestamp,
let submitdate = dictionary["submitdate"] as? Timestamp
else {return nil}
self.init(playerid: playerid, opponent: opponent, season: season, gamedate: gamedate, submitdate: submitdate)
}
}