iphonebackgroundalertbackground-threadcfrunloop

Modal alert in background thread in iPhone


In my iPhone app, main thread creates a background thread which does a lot of work and in some cases needs to ask user for their decision. When user is asked the question, background thread should stop working and should continue only after user has answered question.

What is the best way to do it?

P.S.
I've tried ModalAlert from iPhone Developer's Cookbook, which was said to do the trick, but I have not succeeded. It seems like CFRunLoopRun (which is supposed to stop thread from executing) just does not stop the background thread.

I had code like that

+(void) main
{
    [NSThread detachNewThreadSelector:@selector(startSync) toTarget:self withObject:nil];
}

+(void) startSync
{
    CFRunLoopRun();
    NSLog("Hi");
}

and NSLog was just immediately executed after start of syncStart thread. Weird.


Solution

  • Have you tried NSObject's performSelectorOnMainThread:withObject:waitUntilDone: method?

    Present your question to the user on the main thread, then store the response somewhere where your background thread can access it. Also, you should be able to block your background thread for the duration that your main thread selector runs by setting YES to the waitUntilDone: parameter.