Swift Alternative to Performselectoronmainthread

Swift alternative to performSelectorOnMainThread

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()?

How to perform a selector on the main thread with the AppDelegate in a global function, in swift?

performSelectorOnMainThread:withObject:waitUntilDone: queues the message with common run loop modes. According to Apple's "Concurrency Programming Guide", the main queue will interleave queued tasks with other events from the app's run loop. Thus, if there are other events to be processed in the event queue, the queued blocks in the dispatch queue may be run first, even though they were submitted later.

To resolve the issue you can use dispatchQueue for this as follows:

DispatchQueue.main.async {
UIApplication.shared.delegate.removeSource()
}

You can read more about this on following link:
https://blackpixel.com/writing/2013/11/performselectoronmainthread-vs-dispatch-async.html

Swift - Using performSelectorOnMainThread

Why not

self.window.beginSheet(progSheet) {(returnCode) -> Void in
dispatch_async(dispatch_get_main_queue()) {
progInd.startAnimation(self)
self.readData(path)
progInd.indeterminate = true
}
}

At some point you have to call self.window.endSheet(progSheet) to dismiss the sheet and call the completion handler.

Edit:

I guess you actually mean something like this

...
self.window.beginSheet(progSheet) {(returnCode) -> Void in
progInd.stopAnimation(self)
progInd.indeterminate = true

}

progInd.startAnimation(self)
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority,0)) {
self.readData(path) {
dispatch_async(dispatch_get_main_queue()) {
self.window.endSheet(progSheet)
}
}
}
}

func readData(path:String, completion: (() -> Void)) {
print("Hello!?")
completion()
}

iOS Swift 3 performSelector onMainThread

is not in withObject it is in with

try this

performSelector(onMainThread: #selector(importComplete), with: nil, waitUntilDone: false)

and called the method as

func importComplete() {
//
print("Hello World")
}

output
Sample Image

iOS performSelectorOnMainThread with multiple arguments

When you're using iOS >= 4, you'd do this instead:

dispatch_async(dispatch_get_main_queue(), ^{
[self doSomething:1 b:2 c:3 d:4 e:5];
});

That's like doing waitUntilDone:NO. If you want to wait until the method is finished, use dispatch_sync instead.



Related Topics



Leave a reply



Submit