I want to reload my table data inside a block in this method:
import UIKit
import AssetsLibrary
class AlbumsTableViewController: UITableViewController {
Swift 3 / Xcode 8.2
Here is a Swift 3 way of doing things. Simply insert the time (in seconds) that you would like the code to execute after.
let delayTime = DispatchTime.now() + Double(Int64(20.0 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
DispatchQueue.main.asyncAfter(deadline: delayTime) {
//Your code to run after 20 seconds
}
Alternatively you could simply place your time delay in one-line notation like so:
DispatchQueue.main.asyncAfter(deadline: .now() + 10.0) {
// Your code to run after 10 seconds
}
Swift 3
DispatchQueue.main.async(execute:
{
//Code to execute on main thread
})
You are calling performSelectorOnMainThread on UIViewController instead of UITableView's object
May be your code:
self.performSelectorOnMainThread(Selector(reloadData), withObject: self.tblMainTable, waitUntilDone: true)
Instead of:
self.tblMainTable.performSelectorOnMainThread(Selector("reloadData"), withObject: nil, waitUntilDone: true)
you are getting that message because UIViewController does't have any method named "performSelectorOnMainThread"
//Xcode 8.2 // swift 3.0
I am accessing an API which gives the data regarding weather i.e 'temperature', 'humidity', 'pressure' etc after submitting the name or zipcode of any city. So, That data need to be shown(It must be on Main Thread) on UI (the tableView)
self.performSelector(onMainThread: #selector(Home.DataOnUI), with: nil, waitUntilDone: true)
//
func DataOnUI() {
self.tblView.reloadData()
}
P.S. Home is the class of UIViewController
This simple C-function:
dispatch_async(dispatch_get_main_queue(), {
// DO SOMETHING ON THE MAINTHREAD
self.tableView.reloadData()
})
What about launching your function with:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
loadAlbums()
})
in viewDidLoad()?
Use GCD in lieu of performSelector variations.
dispatch_async(dispatch_get_main_queue()) {
() -> Void in
self.doSomething()
}