如何知道其他线程中的任务何时完成?

时间:2013-10-29 02:01:18

标签: ios iphone multithreading asynchronous concurrency

我想在其他线程(不是主线程)中解析一些数据

for (NSString* theKey in [rssSourcesData allKeys])
 {
      NSURL *url = [NSURL URLWithString:theKey];
      NSURLRequest *initialRequest = [NSURLRequest requestWithURL:url];
     AFGDataXMLRequestOperation *oper = [AFGDataXMLRequestOperation
XMLDocumentRequestOperationWithRequest:initialRequest 
success:^(NSURLRequest *request, NSHTTPURLResponse *response, GDataXMLDocument *XMLDocument) {
            [self parseDataInBackground:XMLDocument forKey:theKey];

        } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, GDataXMLDocument *XMLDocument) {

            NSLog(@"failure handler: %@",theKey);

        }];

        [oper start];
}

finshied解析其他线程中的所有数据后,我想返回主线程。怎么做?

2 个答案:

答案 0 :(得分:1)

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // do your work here

    dispatch_async(dispatch_get_main_queue(), ^{
        // on the main thread
    }) ;
}) ;

gcd更有效。

答案 1 :(得分:0)

 [self performSelectorOnMainThread:@selector(parseFinished:) withObject:dataObject waitUntilDone:[NSThread isMainThread]];
  

一旦结束解析数据,请使用此方法返回主线程并通知数据。