why CFRunLoopRun does not work?

2.3k views Asked by At
[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?

3

There are 3 answers

3
joerick On

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.

0
wupei On

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. :(

0
zoul On

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];
    });
}