import UIKit
import Firebase
class LeaderBoardViewController: UIViewController {
struct Person{
var placePoints:Int
var placeNames:String
}
var firstPlace = Person(placePoints:2,placeNames:"")
var secondPlace = Person(placePoints:1,placeNames:"")
var thirdPlace = Person(placePoints:1,placeNames:"")
var check = 0
var uid = ""
var uniqueNumber:Int?
var documentdata:[String:Any] = ["":""]
@IBOutlet weak var firstPlaceLabel: UILabel!
@IBOutlet weak var secondPlaceLabel: UILabel!
@IBOutlet weak var thirdPlaceLabel: UILabel!
@IBOutlet weak var reloadLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
createLeaderboard()
// Do any additional setup after loading the view.
}
func createLeaderboard(){
let db = Firestore.firestore()
db.collection("users").document("0UniqueNumber").getDocument { (document,error) in
print("wheat the he")
if error != nil{
print("cant get data")
}
if document != nil && document!.exists{
print("its here")
if let documentdata = document?.data() {
self.uniqueNumber = documentdata["tally"] as? Int
print(self.uniqueNumber)
print("FDSFSF")
self.rat()
}
}
}
}
func rat(){
//var firstPlacePoints:Int?
//var firstPlaceName:String?
//var secondPlacePoints:Int?
//var secondPlaceName:String?
//var thirdPlacePoints:Int?
//var thirdPlaceName:String?
let db = Firestore.firestore()
for index in 0...self.uniqueNumber!-1{
let indexString = String(index)
db.collection("leaderboard").document(indexString).getDocument { (document,error) in
if error != nil{
print("cant get data")
}
if document != nil && document!.exists{
if let documentdata = document?.data() {
self.check = documentdata["points"] as! Int
self.uid = documentdata["uid"] as! String
print("ahwehfesfhsadfsfdsdfsdfsdfsdf")
if (self.check > self.firstPlace.placePoints){
self.secondPlace.placePoints = self.firstPlace.placePoints
self.secondPlace.placeNames = self.firstPlace.placeNames
self.firstPlace.placePoints = self.check
self.firstPlace.placeNames = self.uid
print("hi")
}else if(self.check < self.firstPlace.placePoints && self.secondPlace.placePoints < self.check){
self.thirdPlace.placePoints = self.secondPlace.placePoints
self.thirdPlace.placeNames = self.secondPlace.placeNames
self.secondPlace.placePoints = self.check
self.secondPlace.placeNames = self.uid
}else if(self.check < self.secondPlace.placePoints && self.thirdPlace.placePoints < self.check){
self.thirdPlace.placePoints = self.check
self.thirdPlace.placeNames = self.uid
}
}
}
}
//setGuys()
}
print(self.firstPlace.placePoints)
}
}
最后,当我打印self.firstPlace.placePoints时,它会在初始化时打印原始值。它会更改if语句中的值,然后使用print语句进行检查。但是就像我说的那样,当我运行print语句时,它不会更新。这是否与局部变量有关?谢谢您的帮助。
答案 0 :(得分:0)
问题是您正在循环运行并正在调用异步方法。循环可能在异步方法完成之前完成。您应该等到异步方法完成后才能再次调用它。因此,可能会发生以下情况:
func createLeaderboard() {
let db = Firestore.firestore()
db.collection("users").document("0UniqueNumber").getDocument { document, error in
guard let document = document,
let documentData = document.data(),
let tally = documentData["tally"] as? Int,
document.exists,
error == nil else {
print("Failed")
return
}
self.rat(index: 0, tally: tally)
}
}
func rat(index: Int, tally: Int) {
guard index < tally else {
return
}
let indexString = String(index)
let db = Firestore.firestore()
db.collection("leaderboard").document(indexString).getDocument { document, error in
guard let document = document,
let documentData = document.data(),
document.exists,
error == nil else {
print("Failed")
return
}
guard let check = documentData["points"] as? Int,
let uid = documentData["uid"] as? String else {
print("Missing data")
return
}
// ...
// Do whatever else you have to do here
// ...
// Now you're ready make the next call
self.rat(index: index + 1, tally: tally)
}
}