0

如何在viewDidLoad的后台线程中执行 JSON 解析,并在单击按钮时将其移动到主线程。现在我试过了

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


-(void)parseData    {

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

}

但我不知道如何将此后台进程更改为主线程。请帮我

4

2 回答 2

0

你想在主线程上运行的那个方法的部分,只要把它放在这个 GCD 块里面,它就会在主线程上运行它:

dispatch_sync(dispatch_get_main_queue(), ^{
    //put stuff here
});
于 2013-09-10T10:50:23.227 回答
0

请参阅 NSThread 中的 NSObject (NSThreadPerformAdditions)

 - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait modes:(NSArray *)array;
 - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait;
于 2013-09-10T11:19:10.093 回答