0

I want to parse some data in other thread (not main thread)

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

After finshied parse all data in other thread, I want to return back main thread. How to do that ?

DungLe
  • 265
  • 1
  • 3
  • 10
  • you can use blocks in the code to force the app to return to the main thread. alternatively, you can add a listener and post a NSNotification when the parseDataInBackground is completed, so that the listener can execute the codes after. this link may help in understanding: http://stackoverflow.com/questions/10492138/ios-what-is-the-equivalent-of-an-event-listener-in-objective-c – faterpig Oct 29 '13 at 02:35

2 Answers2

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 is more effective.

KudoCC
  • 6,912
  • 1
  • 24
  • 53
  • Did you mean: `dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // do your work here dispatch_async(dispatch_get_main_queue(), ^{ // on the main thread }) ; }) ;` – DungLe Oct 29 '13 at 04:19
  • yes, tell the truth i didn't know much about your code, but i think the code I listed will help you. You can modify your code to fit the condition. – KudoCC Oct 29 '13 at 05:09
  • here is the link about GCD [Concurrency Programming Guide](https://developer.apple.com/library/mac/documentation/General/Conceptual/ConcurrencyProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008091) – KudoCC Oct 29 '13 at 05:10
0
 [self performSelectorOnMainThread:@selector(parseFinished:) withObject:dataObject waitUntilDone:[NSThread isMainThread]];

Once you end parsing data use this method to return to main thread and notify with data.

HDG
  • 32
  • 1
  • 1