Iphone 为什么CFRunLoopRun不起作用?
给定前面的代码,您知道为什么Iphone 为什么CFRunLoopRun不起作用?,iphone,objective-c,ios,cfrunloop,Iphone,Objective C,Ios,Cfrunloop,给定前面的代码,您知道为什么CFRunLoopRun()不工作吗?。我需要在后台调用regFun 是否有其他方法停止后台线程?regFun位于后台线程中,因此对CFRunLoopRun()的调用在此线程中创建并运行一个运行循环。只是运行循环没有附加任何内容,因此它会立即退出。它可以工作 [self request]; //main thread - (void)request { [self performSelectorInBackground:@selector(regFun) w
CFRunLoopRun()
不工作吗?。我需要在后台调用regFun
是否有其他方法停止后台线程?
regFun
位于后台线程中,因此对CFRunLoopRun()
的调用在此线程中创建并运行一个运行循环。只是运行循环没有附加任何内容,因此它会立即退出。它可以工作
[self request]; //main thread
- (void)request {
[self performSelectorInBackground:@selector(regFun) withObject:nil];
}
- (void)regFun {
CFRunLoopRun();
CCLOG(@"CFRunLoopRun not work");
}
但我不确定这是正确的方法,我也不知道发生了什么(好的,因为您没有告诉我们您真正需要做什么,让我们猜猜。如果您只是想在后台运行选择器,请尝试Grand Central Dispatch:
[self request]; //main thread
- (void)request {
//[self performSelectorInBackground:@selector(regFun) withObject:nil];
[NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(regFun) userInfo:nil repeats:NO];
}
- (void)regFun {
CFRunLoopRun();
CCLOG(@"CFRunLoopRun not work");
}
你到底想做什么?很可能有更简单的方法。检查。我不明白。如果我这样做。CFRunLoopRun()可以工作。[自执行选择器:@selector(regFun)];但我需要在后台调用regFun。因为我希望主线程继续运行。你说运行循环没有附加任何内容是什么意思?即使没有挂起的操作,它也应该运行吗?例如,等待新操作?@paiego不,这不是它的工作方式。如果没有附加任何内容,运行循环将退出。你可以将计时器连接到它,它将无限期地保持运行。
- (void) request {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self regFun];
});
}
- (void) regFun {
// Running in background, with a run loop available
// and an autorelease pool, too. After the code finishes:
dispatch_async(dispatch_get_main_queue(), ^{
// Will be called on the main thread.
[self reportBackgroundTaskFinished];
});
}