Search code examples
swiftfunctioncompletionhandlercompletion

Completionhandling in webrequest session


I´ve a webrequest with jsonserialization, after that, a for-in fetch process. In whole this takes approximately 5-7 seconds. After that i want to refersh my tableview in Viewcontroller. The scheme of the function looks like this.

public struct Container {
    let name: String
    let symbol: String
    let rank: String
}

public var dataArray = [Container]()


func fetchNewData() {

    var view = ViewController()

    // WebbRquest...

    // Json serialization...

    // the following list is much longer, will take a while...
    for items in json {
        let name = items["name"] as? AnyObject;
        let symbol = items["symbol"] as? AnyObject;
        let rank = items["rank"] as? AnyObject;
        let result = Container(name: name! as! String, symbol: symbol! as! String,rank: rank! as! String)

        dataArray.append(result)
    }

    // Now, after alle the work is done, i want to reload the tableview in Viewcontrller:
    view.reload()

    // Here i´m getting error, because nothing will be executed after return.

}

How can I call the reload function, after the webrequest process is finished? Because after the return, the function doesn´t execute anything anymore. And no other function will "know" when the fetchNewData() function is finished. Thanks for any help!

@IBAction func updateButton(_ sender: Any) {

     fetchNewData()

    }

According Phillipps suggestion, I had to modify the @IBAction func a little bit. But now it´s working. Awesome! Here the full working version:

public struct Container {
    let name: String
    let symbol: String
    let rank: String
}

public var dataArray = [Container]()


func fetchNewData(completion:@escaping ([Container])->()) {

    var view = ViewController()

    // WebbRquest...

    // Json serialization...

    // the following list is much longer, will take a while...
    for items in json {
        let name = items["name"] as? AnyObject;
        let symbol = items["symbol"] as? AnyObject;
        let rank = items["rank"] as? AnyObject;
        let result = Container(name: name! as! String, symbol: symbol! as! String,rank: rank! as! String)

        dataArray.append(result)
    }

    completion(dataArray)


}

This is the actionFunc:

 @IBAction func upDateButton(_ sender: Any) {

            let data = dataArray

            fetchNewData() {_ in (data)

            DispatchQueue.main.async {
                self.tableView.reloadData()
            }

 }

Solution

  • Here's a start. It will be vague because I'm making guesses about code I can't see, but you may be able to convert it to your own needs.

    Change the fetch function so that it takes a closure as a parameter:

    func fetchNewData(completion:([Container])->()) {
    

    ...note that the closure will accept the data array when it's called. After you have your json all parsed, you then invoke the closure:

        dataArray.append(result)
    }
    completion(dataArray)
    

    The "magic" is in the view controller where you tell fetchNewData what to do when it's finished. Something like:

    @IBAction func updateButton(_ sender: Any) {
         fetchNewData() {(data)
            // Save the data where the view controller can use it
            self.tableArray = data
    
            // Main queue for UI update
            DispatchQueue.main.async {
                self.tableView.reloadData()
            }
         }
    }
    

    Note that the closure is written in the view controller, so self is the view controller. This means no need to create a second (useless) controller inside the fetch.