2

I have a ViewController with a TableView in it, now the data for TableView is pulled via an http call asynchronously in another class.

class ViewController1: UIViewController, UITableViewDelegate, UITableViewDataSource {

    override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

            MyHttpClass().getData()//INITIATING getting data asynchronously
...
}

MyHttpClass gets the data after a few seconds asynchronously, but it does not have a reference of the ViewController to hand over the data (ideally the ViewController.tableView.reloadData - is what i need to call)

Is there a better way to handle this? How do i get ref. to ViewController instance when MyHttp gets the data? Any better way to handle this?

Sanjay Mohnani
  • 5,947
  • 30
  • 46
Jasper
  • 8,440
  • 31
  • 92
  • 133

2 Answers2

3

Well, I suggest you to use call back,add a input as this

 func getData(callBack:()->()){
    //When finished
    callBack()
}

Then call

MyHttpClass().getData() { () -> () in
        self.tableView.reloadData()
    }
teedyay
  • 23,293
  • 19
  • 66
  • 73
Leo
  • 24,596
  • 11
  • 71
  • 92
2

The best way to solve this problem would be to make MyHttpClass().getData() take a callback to execute when it finishes loading the data asynchronously. In this callback you can then reload the tableview.

duncanc4
  • 1,191
  • 1
  • 9
  • 17