Skip to content

Latest commit

 

History

History
52 lines (35 loc) · 1.3 KB

dispatch-main-queue.md

File metadata and controls

52 lines (35 loc) · 1.3 KB

How dispatch main queue

With delay

DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(delay)) {
	// delayed main thread
}

Getting on the main thread

We always want to be rendering and updating components on the main UI thread. That includes all UITable reloads() and calculations from delegates.

One way to do that that always happens, is when you call a long lived asynchronous operation, make sure you put the competion block your return back on the main thread.

For example if we make a asynchronous network call like this

ViewController

   func fetchData() {
        Service.shared.fetchCourses { (courses, err) in

            self.tableView.reloadData() // want to make sure on main thread!
        }
    }

We can make sure the callback is put onto the main thread like this

Services

class Service: NSObject {
    static let shared = Service()
    
	func fetchCourses(completion: @escaping ([Course]?, Error?) -> ()) {
	
	    // network call... parsing ...
	    DispatchQueue.main.async {
	        completion(courses, nil)
	    }
	}
}

You can do it here, or in the ViewController. Doesn't really matter. So long as you do it.

Links that help