Swift 3 - viewdidload 中的函数未完全执行

Swift 3 - function in viewdidload not completly executing

我在 viewDidLoad 中使用了一个附加数组的函数,并且该数组在其他 function.But 中使用,当我执行我的程序时 viewDidLoad 中的函数没有完全执行所以我的 array 仍然是 empty.Please 帮助我。

我的代码:-

 override func viewDidLoad() {
    super.viewDidLoad()
    self.getAllUsers()
    self.fetchData()
}
 func getAllUsers(){
    ref = Database.database().reference()
    ref.child("users").observe(.value, with: { (snapshot) in
        if let result = snapshot.children.allObjects as? [DataSnapshot] {
            for child in result {
                var userid = child.key
                print(userid)
                self.uuid.append(userid)
            }
        }
        if self.uuid.contains((Auth.auth().currentUser?.uid)!) {
            print("Id matched")
        }
    })

    let array = ["Frodo", "sam", "wise", "gamgee"]
    let randomIndex = Int(arc4random_uniform(UInt32(array.count)))
    print(array[randomIndex])

}
 func fetchData() {
    let randomIndex = Int(arc4random_uniform(UInt32(uuid.count)))
    print(uuid[randomIndex])
    print(randomIndex)
    randomI = randomIndex
    Message.downloadAllMessages(forUserID: uuid[randomIndex], completion: {[weak weakSelf = self] (message) in
        weakSelf?.items.append(message)
        weakSelf?.items.sort{ [=11=].timestamp < .timestamp }
        DispatchQueue.main.async {
            if let state = weakSelf?.items.isEmpty, state == false {
                weakSelf?.tableView.reloadData()
                weakSelf?.tableView.scrollToRow(at: IndexPath.init(row: self.items.count - 1, section: 0), at: .bottom, animated: false)
            }
        }
    })
    Message.markMessagesRead(forUserID: uuid[randomIndex])
}

尝试在完成块后调用 self.fetchData()。您正在调用self.fetchData()之前完成。

 override func viewDidLoad() {
    super.viewDidLoad()
    self.getAllUsers()

}
 func getAllUsers(){
    ref = Database.database().reference()
    ref.child("users").observe(.value, with: { (snapshot) in
        if let result = snapshot.children.allObjects as? [DataSnapshot] {
            for child in result {
                var userid = child.key
                print(userid)
                self.uuid.append(userid)
            }
        }

        if self.uuid.contains((Auth.auth().currentUser?.uid)!) {
            print("Id matched")
        }
        self.fetchData()// call your method after compeletion block
    })

    let array = ["Frodo", "sam", "wise", "gamgee"]
    let randomIndex = Int(arc4random_uniform(UInt32(array.count)))
    print(array[randomIndex])

}
 func fetchData() {
    let randomIndex = Int(arc4random_uniform(UInt32(uuid.count)))
    print(uuid[randomIndex])
    print(randomIndex)
    randomI = randomIndex
    Message.downloadAllMessages(forUserID: uuid[randomIndex], completion: {[weak weakSelf = self] (message) in
        weakSelf?.items.append(message)
        weakSelf?.items.sort{ [=10=].timestamp < .timestamp }
        DispatchQueue.main.async {
            if let state = weakSelf?.items.isEmpty, state == false {
                weakSelf?.tableView.reloadData()
                weakSelf?.tableView.scrollToRow(at: IndexPath.init(row: self.items.count - 1, section: 0), at: .bottom, animated: false)
            }
        }
    })
    Message.markMessagesRead(forUserID: uuid[randomIndex])
}