Search code examples
iphoneuiviewsynchronousnavigationbartitleview

iPhone navigationBar titleView sync request problem


Here's my situation: I am making synchronous HTTP requests to collect data but before hand I want to place a loading view within the navigation bar title view. After the request is over I want to return the titleView back to nil.

[self showLoading];        //Create loading view and place in the titleView of the nav bar.
[self makeHTTPconnection]; //Creates the synchronous request
[self endLoading];         //returns the nav bar titleView back to nil.

I know the loading view works because after the request is over the loading view is shown.

My Problem: It should be obvious at this point but basically I want to delay the [self makeHTTPconnection] function until the [self showLoading] has completed.

Thanks for you time.


Solution

  • You can't do that in a synchronous approach. When you would send [self showLoading] message, the UI wouldn't be updated until the entire method finishes, so it would already finish the other two tasks (makeHTTPConnection and endLoading). As a result, you would never see the loading view.

    A possible solution for this situation would be working concurrently:

    [self showLoading];
    NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(_sendRequest) object:nil];
    [queue addOperation:operation];
    [operation release];
    

    Then you must to add the *_sendRequest* method:

    - (void)_sendRequest
    {
        [self makeHTTPConnection];
        //[self endLoading];
        [self performSelectorOnMainThread:@selector(endLoading) withObject:nil waitUntilDone:YES];
    }