IOS Swift 3 fetch onMainThread

I used this line in Objective-C

[self performSelectorOnMainThread: @selector(importComplete)
withObject: nil waitUntilDone: YES];

      

I've tried converting the above string to any number of options, but couldn't find anything that will compile. My current attempt is as follows:

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

      

I was unable to compile my changes. The above change delivers the message

"Use of an unauthorized identifier performSelector

.

I've read everything I can find in this method and it looks like a mess in Swift 3 anyway, but I'm just learning the language and I've spent a lot of time on what should be simple syntax.

Can anyone make a suggestion here?

TIA

+3


source to share


4 answers


is not in withObject

, it is inwith

try it

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

      



and called the method as

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

      

Output enter image description here

+2


source


One of the options could be:

let selector = NSSelectorFromString("importComplete")
perform(selector, on: Thread.main, with: nil, waitUntilDone: true)

      

or



let selector = NSSelectorFromString("importComplete")
perform(selector, on: Thread.main, with: nil, waitUntilDone: true, modes: nil)

      

Depending on your needs.

+1


source


You can use this code to perform an operation on main

thread

performSelector(onMainThread: #selector(performOperation), with: nil, waitUntilDone: true)

      

Your method of choice

func performOperation() {

    }

      

+1


source


If you declare your function as,

func importComplete()  {


}

      

then you can do,

   self.performSelector(onMainThread: #selector(ViewController.importComplete), with: nil, waitUntilDone: true)

      

where ViewController

is your current class!

0


source







All Articles