0

How can i perform a JSON parsing in background thread in viewDidLoad and move it to main thread when clicks on a button. Now i tried

[self performSelectorInBackground:@selector(parseData) withObject:nil];


-(void)parseData    {

    MSJsonParser *parser = [[MSJsonParser alloc]initWithParserType:kCountriesParsing];
    parser._parserSource = self;
    [parser requestParsingWithUrl:COUNTRIES_URL ];

}

But i dont know how to change this background process to main thread. please help me

manujmv
  • 6,450
  • 1
  • 22
  • 35

2 Answers2

0

The part in that method you want to run on the main thread, just stick it inside this GCD block and it will run it on the main thread:

dispatch_sync(dispatch_get_main_queue(), ^{
    //put stuff here
});
Fonix
  • 11,447
  • 3
  • 45
  • 74
  • not sure if i get what you mean by parse it in the background and click on a button to make it go to main thread though... sounds like you are trying to get a method to potentially run on 2 different threads.. – Fonix Sep 10 '13 at 10:53
  • Doesn't `dispatch_sync` block the thread that made the call while it executes the block? – JeremyP Sep 10 '13 at 12:55
0

See NSObject (NSThreadPerformAdditions) in NSThread

 - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait modes:(NSArray *)array;
 - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait;
Jason
  • 59
  • 5