gpt4 book ai didi

ios - 适当的线程处理

转载 作者:行者123 更新时间:2023-11-29 00:26:35 24 4
gpt4 key购买 nike

我遇到了一个问题,需要适当的线程,但我似乎无法正确优化它。

这是我的方法:

-(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]
}

在[selffreshTableSessionList: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)

之前[在这里分配数据库等内容并检索数据]
并放入 dispatch_async(dispatch_get_main_queue(),
当按钮变灰时,但这是在循环内,我认为这不是正确的方法。

解决这个问题的解决方案是什么?

最佳答案

据我了解,您不会等待后台数据库的完成。

您了解过多线程吗?例如,Ray's article .

简单来说,您可以在dispatch_async等的dipatch_async block 内调用dispatch_async。

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// do some database stuff
dispatch_async(dispatch_get_main_queue(), ^{
// do some UI stuff
});
});

所以你应该在主线程和全局队列之间切换。此外,您还可以使用委托(delegate)、通知甚至 react 性来实现此类目的。

关于ios - 适当的线程处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42851560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com