简体   繁体   中英

can we change a background thread operation to main thread?

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

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM