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
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
Ios 15 Navigation Bar Transparent
Saving Webview to Pdf Returns Blank Image
How to Access Nswindow from @Main App Using Only Swiftui
The Strange Behaviour of Swift's Anyobject
Swift Write/Save/Move a Document File to Icloud Drive
How to Replicate Promisekit-Style Chained Async Flow Using Combine + Swift
How We Can Get and Read Size of a Text with Geometryreader in Swiftui
Swift Watchos 2 - Cmsensordatalist
Custom Segue Transition Animation
Using Some Protocol as a Concrete Type Conforming to Another Protocol Is Not Supported
Swap Rootviewcontroller With Animation
Why .Pch File Not Available in Swift
Take a Snapshot of Current Screen with Metal in Swift
Navigationview Pops Back to Root, Omitting Intermediate View
Init(Start:End:)' Is Deprecated: It Will Be Removed in Swift 3. Use the '..<' Operator
How to Override Trait Collection for Initial Uiviewcontroller? (With Storyboard)
Obervableobject Being Init Multiple Time, and Not Refreshing My View