您的位置:首页 > 产品设计 > UI/UE

NSOperationQueue 多线程

2013-11-26 21:51 267 查看
staticNSOperationQueue * queue;

- (void)viewDidLoad

{

  [superviewDidLoad];

  queue = [[NSOperationQueuealloc] init];

 NSInvocationOperation * op = [[NSInvocationOperationalloc] initWithTarget:selfselector:@selector(download) object:nil];

 [queueaddOperation:op];

}

- (void)download {

NSURL * url = [NSURLURLWithString:@"http://www.youdao.com"];

NSError * error;

NSString * data = [NSStringstringWithContentsOfURL:url encoding:NSUTF8StringEncodingerror:&error];

if (data != nil) {

[selfperformSelectorOnMainThread:@selector(download_completed:) withObject:data waitUntilDone:NO];

} else {

NSLog(@"error when download:%@", error);

queue = nil;

}

}

- (void) download_completed:(NSString *) data {

NSLog(@"call back");

self->contentLabel.text = data;

queue = nil;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: