Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

performSelector:onThread: in Swift?

In a current iOS app I am using this perform selector approach:

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

I am not sure how to make a selector run on a specific thread in swift. Any suggestions?

like image 313
zumzum Avatar asked Jul 02 '14 22:07

zumzum


1 Answers

As I suggested in comment, you shouldn't manage threads any more. Always use dispatch_queue instead of threads.

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

This is C code, but I think you can translate it to Swift code without too much work.

// 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();
});
like image 96
Bryan Chen Avatar answered Sep 27 '22 18:09

Bryan Chen