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];
}

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

4

2 回答 2

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

于 2013-10-29T02:55:04.923 回答
0
 [self performSelectorOnMainThread:@selector(parseFinished:) withObject:dataObject waitUntilDone:[NSThread isMainThread]];

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

于 2013-10-29T02:46:10.107 回答