2013-10-29 58 views
0

我想分析在其他线程的一些数据(没有主线程)如何知道其他线程中的任务何时完成?

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解析后在其他线程中的所有数据,我想回到回主线程。怎么做 ?

+0

您可以在代码中使用块来强制应用程序返回到主线程。或者,您可以在parseDataInBackground完成时添加侦听器并发布NSNotification,以便侦听器可以在之后执行代码。此链接可能有助于理解:http://stackoverflow.com/questions/10492138/ios-what-is-the-equivalent-of-an-event-listener-in-objective-c – faterpig

回答

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

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

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是更有效的。

+0

我做到了,但它不工作。 – DungLe

+0

你是什么意思,它不工作? – KudoCC

+0

在任务完成之前,它会在主线程上返回! – DungLe

相关问题