How to make NSRunLoop work inside a separate thread?

亡梦爱人 提交于 2019-12-06 05:19:17

问题


Please look at this code:

@interface myObject:NSObject

-(void)function:(id)param;

@end

@implementation myObject

-(void)function:(id)param
{
    NSLog(@"BEFORE");
    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:20]];
    NSLog(@"AFTER");
}

@end


int main(int argc, char *argv[])
{
    myObject *object = [[myObject alloc] init];

    [NSThread detachNewThreadSelector:@selector(function:) toTarget:object withObject:nil];

    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

The function method is called but there's no 20 seconds pause. What should i do to make NSRunLoop work in a detached thread?


回答1:


Since you are running the function: selector in a different thread, [NSRunLoop currentRunLoop] is not the same as in the main thread.

Please see NSRunLoop reference:

If no input sources or timers are attached to the run loop, this method exits immediately

I guess that your run loop is empty, and therefore the "BEFORE" and "AFTER" logs will appear instantaneously.

A simple solution to your problem would be

@implementation myObject

-(void)function:(id)param
{
  NSLog(@"BEFORE");
  [[NSRunLoop currentRunLoop] addTimer:[NSTimer timerWithTimeInterval:20 selector:... repeats:NO] forMode:NSDefaultRunLoopMode];
  [[NSRunLoop currentRunLoop] run];
  NSLog(@"AFTER");
}

@end

In reality, you would probably put the code that logs "AFTER" in a new method that is called by your timer. In general, you do not need threads to do animations (unless you are doing something computationally expensive). If you are doing computationally expensive stuff, you should also look into using Grand Central Dispatch (GCD), which simplifies off-loading calculations on background threads and will handle the plumbing for you.



来源:https://stackoverflow.com/questions/9819872/how-to-make-nsrunloop-work-inside-a-separate-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!