wupei
wupei

Reputation: 21

why CFRunLoopRun does not work?

[self request]; //main thread

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

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

Given the previous code, do you know why CFRunLoopRun() is not working?. I need to call regFun in background.

Are there any other ways to stop background thread?

Upvotes: 1

Views: 2394

Answers (3)

zoul
zoul

Reputation: 104065

OK, since you are not telling us what you really need to do, let’s guess. If you just want to run a selector in the background, try 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];
    });
}

Upvotes: 1

wupei
wupei

Reputation: 21

It can work.

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

But I'm not sure this is the right approach, and I don't know what happened. :(

Upvotes: 1

joerick
joerick

Reputation: 16448

regFun is in a background thread, so the call to CFRunLoopRun() creates and runs a run loop in this thread. Only there's nothing attached to the run loop, so it exits immediately.

Upvotes: 0

Related Questions