iphone上的异步与同步方法

iphone上的异步与同步方法,iphone,Iphone,我看到有一种同步方法,比如如果我想做以下事情: -(IBAction)doNSURLConnSync { NSURLRequest *request = [NSURLRequest requestWithURL:url]; NSError *error = nil; NSURLResponse *response = nil; [NSURLConnection sendSynchronousRequest:request

我看到有一种同步方法,比如如果我想做以下事情:

-(IBAction)doNSURLConnSync {
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSError *error = nil;
    NSURLResponse *response = nil;
    [NSURLConnection sendSynchronousRequest:request                                      returningResponse:&response                                                            error:&error];
}
它的性能与异步执行时有何不同:

-(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];
}
另外,使用doNSURLConnSync方法,我只是尝试加载UIWebView。有没有理由不这样做?与异步版本相比,该按钮只是停留在那里并保持高亮显示,同时它尝试访问网页,但最终什么也不做


另外,对于异步代码中的networkactivityindicator,我希望将UIWebView设置为空,在加载webview时打开该指示器,然后在加载页面后关闭网络活动指示器。但是,如果我删除loadHTMLString方法,网络活动指示器将按预期工作,但是对于loadHTMLString,UIWebView将变为空白,但网络活动指示器不会。有什么想法吗?谢谢。

首先,对于Synchronous:

+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error
这是方法签名,当您调用同步请求时,它将返回要显示在UIWebView上的数据,您必须调用UIWebView来显示数据。但是,同步调用将阻止用户界面,直到所有数据返回。所以,要小心UX

对于异步,它不会阻塞您的UI,用户仍然可以使用它做任何他们想做的事情,比如返回上一个屏幕。因此,通常情况下,建议用于大型和长网络


我不知道为什么它没有显示你的指示器。但是为什么需要这一行:
[webview loadHTMLString:@”“baseURL:nil]。您只需要在收到HTML响应后调用它,一个同步请求将主线程绑定起来,您应该为UI小部件更新保留主线程

在后台线程上执行异步请求可以释放主线程来更新UI

将UI更新代码(指示器视图和web视图)拉入单独的方法中,在主线程上使用


这是一个类分配,只是为了在加载实际网页之前使webview为空,因为我们以几种不同的方式访问web,并且应该显示不同的方法。嗨,Alex Reynolds,我想在我的代码中实现同样的事情,你能告诉我如何在后台线程上调用同步请求吗?你不能。在后台线程中运行任务的全部目的是使其异步。如果希望同步,请在主线程上运行任务,如上所示。
[self performSelectorOnMainThread:@selector(updateWebview) withObject:nil waitUntilDone:YES];