Search code examples
iphoneiosnsurlconnectionsynchronization

how to create a synchronous NSURLConnection request


I am wanting to create a synchronous nsurlconnection request. Yes I know its stupid and I know the limitations (freezing the UI) however the reason I would like to implement this to garnet the data I am receiving from the server is received in the order it is sent, which Is one of the benefits of a synchronous connection.. if I read correctly.

I am going to be using Grand Central Dispatch to achieve some multi threading in order to run the sync request and not have it effect the UI of the main thread... but this can come later for now I would like to master the sync connection.

The one issue is that I'm not exactly sure how to set my connection as synchronous..

Here is my code.

- (void)loadComics{

    [comics removeAllObjects]; //clears object of any old values
    [[self tableView] reloadData];

    NSURL *url = [NSURL URLWithString:@"http://www.comicbookresources.com/feed.php?feed=previews"];

    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];

    //clear any existing connection
    if (connectionInProgress) {
        [connectionInProgress cancel];
        [connectionInProgress release];
    }

    [xmlData release];
    xmlData = [[NSMutableData alloc] init];

    connectionInProgress = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [xmlData appendData:data];
}

- (void) connectionDidFinishLoading:(NSURLConnection *)connection{

    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xmlData];

    [parser setDelegate:self];

    [parser parse];

    [parser release];
    [[self tableView] reloadData];
}

any examples would be great.


Solution

  • If you want a synchronous connection, you must use +sendSynchronousRequest:returningResponse:error there is no other easy way to do this.

    Note that it's deprecated in favor of NSURLSession, but it doesn't support synchronous connection directly.