PerformSelector: onThread: in Swift?

In the current iOS application, I use this selector selector approach:

[self performSelector:@selector(doSomething)
             onThread:myThread
           withObject:nil
        waitUntilDone:NO
                modes:[NSArray arrayWithObject:NSRunLoopCommonModes]];

I'm not sure how to make selector in a specific thread in swift. Any suggestions?

+4
source share
1 answer

As I said in a comment, you should no longer manage threads. Always use dispatch_queue instead of threads.

If you really want to do this, here is a workaround: CFRunLoopPerformBlock .

This is C code, but I think you can translate it into Swift code without much difficulty.

// worker thread
CFRunLoopRef myrunloop; // some shared variable

void worker_thread_main() {
    myrunloop = CFRunLoopGetCurrent();
    CFRunLoopRun(); // or other methods to run the runloop    
}

// other thread to schedule work

CFRunLoopPerformBlock(myrunloop, kCFRunLoopCommonModes, ^{
    dowork();
});
+6
source

Source: https://habr.com/ru/post/1546927/


All Articles