DispatchQueue.main.async() {
// Code to execute on main thread
}
(Xcode 8, Swift 3.0)
Enjoy this info? Visit my Patreon page to support educators and artists.
My YouTube Channel
Need help with SwiftUI? Check out my products at: Big Mountain Studio Website
DispatchQueue.main.async() {
// Code to execute on main thread
}
// Function with a completion handler parameter
func doWork(completion: @escaping (String) -> () ) {
DispatchQueue.global(qos: .userInitiated).async {
completion("Work is all done!")
}
}
// Function that calls your completion handler function
func startWork() {
print("Starting work")
doWork { (result) -> () in
print(result)
}
print("Waiting for doWork to finish.")
}
Starting work Waiting for doWork to finish. Work is all done!
() -> ()For example, you see this format here:
doWork { (result) -> () in
But there are a few more ways you can write this:
doWork { (result) -> Void in
doWork { (result) in
doWork { (result: String) in
doWork() { result in
doWork { result in
These all work the same. The last one is probably the bare minimum you can get away with.In part 3 of the Searchable video series, I show you how to use Combine in #SwiftUI for the search and filter logic connected to the searcha...