我们可以将后台线程操作更改为主线程吗?

时间:2013-09-10 10:46:34

标签: ios objective-c json background-thread

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

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


-(void)parseData    {

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

}

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

2 个答案:

答案 0 :(得分:0)

要在主线程上运行该方法的部分,只需将其粘贴在此GCD块中,它将在主线程上运行它:

dispatch_sync(dispatch_get_main_queue(), ^{
    //put stuff here
});

答案 1 :(得分: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;