1
[self request]; //main thread

- (void)request {
    [self performSelectorInBackground:@selector(regFun) withObject:nil];
}

- (void)regFun {
    CFRunLoopRun();
    CCLOG(@"CFRunLoopRun not work");
}

前のコードを考えると、なぜ機能しないのか分かりますかCFRunLoopRun()?. バックグラウンドで呼び出す必要がありregFunます。

バックグラウンド スレッドを停止する他の方法はありますか?

4

3 に答える 3

1

それは働くことができます。

[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");
}

しかし、これが正しいアプローチであるかどうかはわかりませんし、何が起こったのかもわかりません。:(

于 2012-01-13T02:57:04.247 に答える
1

OK、あなたは本当に何をする必要があるかを私たちに伝えていないので、推測しましょう. バックグラウンドでセレクターを実行したいだけの場合は、Grand Central Dispatch を試してください。

- (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];
    });
}
于 2012-01-14T09:39:57.170 に答える
0

regFunはバックグラウンド スレッドにあるため、 への呼び出しCFRunLoopRun()はこのスレッドで実行ループを作成して実行します。実行ループには何も接続されていないため、すぐに終了します。

于 2012-01-12T13:55:15.007 に答える