Search code examples
iosarraysswiftviewdidload

Swift 3 - function in viewdidload not completly executing


I have used a function in viewDidLoad that appends an array and that array is used in other function.But when i execute my program the function in viewDidLoad does not completely executing so my array remains empty.Please help me for this.

My code:-

 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{ $0.timestamp < $1.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])
}

Solution

  • Try to call self.fetchData() after completion block. You are calling self.fetchData()before compeletion.

     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{ $0.timestamp < $1.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])
    }