我遇到了一个需要正确线程的问题,但我似乎无法正确优化它。
这是我的方法:
-(void) method1
{
// -1 to an NSInteger
nsint1--;
[self showActiviyIndicator:YES]; //act as loading screen
[alloc database etc stuffs and retrieving of data here]
//for loop here to check with database, and grey out button depending on database values
for (int i = 1; i<12; i ++)
{
//get values from database and store into variables, then grey out the button if variables are 0.
}
int Val1 = [get from database]
if Val1 = 0
[button setTitleColor:[UIColor Grey]];
someLabel.text = [NSString stringWithFormat:@"%ld", (long)nsint1];
//here's where the problem lies
[self refreshTableSessionList:xx];
[self showActiviyIndicator:NO]
}
在 [self refreshTableSessionList:xx] 里面,有一个
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)
从服务器数据库中获取数据,然后
dispatch_async(dispatch_get_main_queue(),
填充和重新加载 tableViewCell。
但是当我放一个
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)
before[alloc database etc stuffs and retrieving of data here]
和 putdispatch_async(dispatch_get_main_queue(),
当按钮变灰时,但那是在一个循环内,我认为这不是正确的方法。
克服这个问题的解决方案是什么?