tags:

views:

100

answers:

2

I saw there is a method for synchronous, like if I wanted to do something like:

-(IBAction)doNSURLConnSync {
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSError *error = nil;
    NSURLResponse *response = nil;
    [NSURLConnection sendSynchronousRequest:request                                      returningResponse:&response                                                            error:&error];
}

How does it perform differently than if I did asynchronous:

-(IBAction)doNSURLConnASync {
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
                                                                delegate:self];
    if (connection) {
        responseData = [[NSMutableData alloc] init];
        [webview loadHTMLString:@"" baseURL:nil];
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                        message:@"Network error occured" 
                                                       delegate:self 
                                              cancelButtonTitle:@"OK" 
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"%s", __FUNCTION__);
}

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

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [connection release]; // got passed in as a param.  we are done with it now.
    [webview loadData:responseData
               MIMEType:nil
       textEncodingName:nil
                baseURL:nil];
    [responseData release];
}

Also, with my doNSURLConnSync method, I am just trying to load a UIWebView. Is there a reason why it doesn't? The button just sits there and stays highlighted, while it tries to access the webpage, but does nothing in the end, compared to the asynchronous version.

Also, for networkactivityindicator in my asynchronous code, I wanted to set my UIWebView to blank, have the indicator on while my webview loads, and then turn off the network activity indicator once the page loads. However, if I delete the loadHTMLString method, the network activity indicator works as it's supposed to, but with the loadHTMLString, the UIWebView goes blank, but the network activity indicator does not. Any thoughts? Thanks.

A: 

First, for the syncrhonous:

+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error

This is the method signature, when you call the synchronous request, it wil return the data for you to display on the UIWebView and you have to call the UIWebView to display the data. However, the synchronous calling will block your UI until all the data come back. So, be careful with UX.

NSUrlConnection sendSynchronousRequest

For the asynchronous, it will not block your UI, user can still do everything they want with it, like go back to the previous screen. So, usually, it is recommended for big and long network

I don't know why it doesn't show your indicator. But why do you need this line : [webview loadHTMLString:@"" baseURL:nil]; . You only need to call it after you got your HTML response

vodkhang
It was for a class assignment just to make the webview blank before loading the actual web page since we accessed the web in a few different ways and were supposed to show the different methods.
Crystal
A: 

A synchronous request ties up the main thread, which you should reserve for UI widget updates.

Doing an asynchronous request on a background thread frees up the main thread to update the UI.

Pull your UI update code (indicator view and web view) into separate methods, calling them on the main thread with -performSelectorOnMainThread:withObject:waitUntilDone:

[self performSelectorOnMainThread:@selector(updateWebview) withObject:nil waitUntilDone:YES];
Alex Reynolds