1

I am trying to use the SVProgressHUD to indicate that the app is busy.

My code is now:

        SVProgressHUD.show()
        DispatchQueue.global(qos: .background).async {
            parser.parse()
            print("start reloading")
            self.protocolTableView.reloadData()

            print("end reloading")
            SVProgressHUD.dismiss()
        }

I get this warning:

UITableView.reloadData() must be used from main thread only

And it takes quite a while before the tableView is displayed after the parsing has been done.

How can I update the tableView after reading in the parsing from the main thread?

Thanks, Bart

R. Mohan
  • 2,182
  • 17
  • 30
  • Not sure if this will work, but can you try putting the show() method in the prepareForSegue method? Or I think you could try running the show() call in the main thread.. – Skywalker Nov 22 '17 at 02:46
  • I updated the code to move it to prepareForSegue(), but it seems that it is only executed on the main thread until the viewDidLoad() has finished. – Bart Schraa Nov 22 '17 at 03:15
  • `SVProgressHUD.show()` already adds itself to main queue, no need to do it explicitly once again. – Nik Yekimov Nov 22 '17 at 03:28

4 Answers4

1

XMLParser.parse can block the UI. Try doing that on a background thread:

DispatchQueue.global(qos: .background).async {
    parser.parse()
}
Tyler Bell
  • 26
  • 3
1

UI updates should be done in main thread:

SVProgressHUD.show()
DispatchQueue.global(qos: .background).async {
    parser.parse()
    print("start reloading")

    DispatchQueue.main.async(execute: {() -> Void in
        self.protocolTableView.reloadData()
        SVProgressHUD.dismiss()
    })
}
clemens
  • 16,716
  • 11
  • 50
  • 65
0
 DispatchQueue.main.async {
        self.protocolTableView.reloadData()
        }

Reload tableView in Main Thread.

0

This was the solution:

SVProgressHUD.show()
DispatchQueue.global(qos: .background).async {
    parser.parse()

    DispatchQueue.main.sync {
        self.protocolTableView.reloadData()
    }

    SVProgressHUD.dismiss()
}

Thanks for all the help!!