Synchronous ASIHTTPRequest updating label
It seems very simple but not easy for me.. I am calling a few Synchronous ASIHTTPRequests and when each request is finished, I want to update a Label like following..
self.status.text = @"Google";
NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
self.status.text = @"Yahoo";
NSURL *url = [NSURL URLWithString:@"http://www.yahoo.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
self.status.text = @"Apple";
NS开发者_高级运维URL *url = [NSURL URLWithString:@"http://www.Apple.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
However, it only show nothing but "Apple" once all calls are done.. What is the simple and best way to achieve this?
UIKit has no chance to redraw the label because you're blocking the main thread for the whole url request.
You should use asynchronous requests instead, so the UI will be responsive all the time. Also update the UI in a completion block:
self.status.text = @"Google";
NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setCompletionBlock:^{
self.status.text = @"Yahoo";
// start another request etc.
}];
[request startAsynchronous];
精彩评论