NSOperationQueue 多线程

staticNSOperationQueue * queue;

- (void)viewDidLoad

{

   [superviewDidLoad];

   queue = [[NSOperationQueueallocinit];

     NSInvocationOperation * op = [[NSInvocationOperationallocinitWithTarget: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;

}

posted @ 2013-11-26 21:51  韦韦韦  阅读(182)  评论(0编辑  收藏  举报