views:

35

answers:

3

Dear iPhone Developers, I am writing a Navigation based iPhone application. I want to execute a piece of code but before the execution I want the viewController to pop up. The method that I want to implement looks as follows;

- (IBAction)executeTheCode {

        // set a BOOLEAN so that the user can not repeat the same action
        // unless the previous run is complete.

        isRunning = TRUE;

        [self.navigationController popViewControllerAnimated:YES];

        // The piece of Code goes here
        //  
}

the code, that is being executed, is iterative and would take some time. I want the application to pop up immediately so that the user can interact with the rest of the stuff meanwhile. In the above case I just put a do loop to check if it works but it does not pop up unless the do loop is complete. Even the back button does not work while the do loop is being executed. Is there any way (if possible at all) to do that? Thanks.

A: 

popViewControllerAnimated

Does the opposite of what you want to do. Pop removes the top view controller from the stack. What you want is (void)viewDidAppear or (void)viewWillAppear method....

In this method you can kick off a progress indicator or do what ever you want to do. For example overlay a waiting UIView while your program is calculating.

What I think you want to do is kick off a thread in one of the above methods that does the work in a background thread.

Check [self performSelectorInBackground:@selector(setup:) withObject:nil];

in the documentation. This will run the method setup in your project in the background, allowing the user to work with the rest of your UIView while -(void)setup runs in the background.

Jordan
Dear Jordan, Thank you very much. I will look in the documentation for the above method and will come back if I have any problem. Thank you very much once again.
UCU110
A: 

Hi, I do something like this;

- (IBAction)executeTheCode {

    // set a BOOLEAN so that the user can not repeat the same action
    // unless the previous run is complete.

    isRunning = TRUE;
    [self updateRightBarButtonItemState];

    [self performSelectorInBackground:@selector(toTheCalculator) withObject:nil];

}

- (void)toTheCalculator {

    NSAutoreleasePool * pool;       
    pool = [[NSAutoreleasePool alloc] init];

    // A Calculation here
    // for the time being a simple do loop

    [pool drain];

    isRunning = FALSE;  
}

and it works as I want it to. But at the end of the process it crashes with a log message

bool _WebTryThreadLock(bool), 0x5b872d0: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...

So I have to figure out what is going wrong. Anyone has any idea? Thanks

UCU110
You can't interact with the UI from a background thread. If you need to change UI elements and you're in the background, you call the mirror-image method you just learned about:
Dan Ray
A: 

You can't interact with the UI from a background thread. If you need to change UI elements and you're in the background, you call the mirror-image method you just learned about: -performSelectorOnMainThread:.

So, you have one method that does the background "thinking", and then passes off its results to another method that runs in the main thread and updates the UI with those results.

Dan Ray
Hi Dan Ray, Thank you very much. I used "performSelectorOnMainThread:" method and now every thing goes fines. thanks a lot.
UCU110