gpt4 book ai didi

ios - 在 foreach 和 dispatch 之后执行操作

转载 作者:行者123 更新时间:2023-11-28 22:09:04 25 4
gpt4 key购买 nike

我正在尝试在 foreach 和我的调度之后执行操作。在 for 循环中,当我遇到一种类型的枚举时,showAlert 将得到 YES。用于在所有任务和循环之后显示警报。

我已经试过了,但是当 dispatch_sync 仍在进行时,警报已经出现了。我该如何解决这个问题?

编辑:问题概述:然后问题是 UIAlertView 在循环完成之前已经出现,所以我没有设置任何值(For 循环对我的 randomInt 执行此操作)并且我的 AlertView 采用此 int。所以这就是为什么我想等到所有循环/foreach 在方法中完成,然后让 UIAlertView 出现,所以我有 100% 我需要的整数。

代码:

-(void)animateRolling{

[[self firstLabelOutlet] setHidden:YES];

long index;
int randomIndex;
__block BOOL showAllert = NO;

randomIndex = arc4random() % 20;

if (randomIndex == 8) {
[self showAd];
}

for(detailDice* cell in [[self dobbelstenenCollection] visibleCells]){

NSIndexPath *indexPath = [[self dobbelstenenCollection] indexPathForCell:cell];

if ([arrayDobbel count] >= 3) {
index = 2 * indexPath.section + indexPath.row;
}else{
index = 1 * indexPath.section + indexPath.row;
}

if ([cell isHidden]) {
[cell setHidden:NO];
}

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
for (int i = 0; i <= 10; i++) {

dispatch_sync(dispatch_get_main_queue(), ^{
if ([[arrayDobbel objectAtIndex:index] diceSoort] == ENUMHoelangDobbel) {

randomInt = ((arc4random() % ([self maxduration] - 1 + 1 )) + 1.0);
[[cell detaildiceLabel] setText:[NSString stringWithFormat:@"Seconds \n %i", (int)randomInt]];

showAllert = YES;

}else if ([[arrayDobbel objectAtIndex:index] diceSoort] == ENUMOgen){

[[cell detailDiceImage] setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%i.png", ( (arc4random() % (6-1+1)) + 1 )]]];

}else{
[[cell detaildiceLabel] setText: [[arrayDobbel objectAtIndex:index] returnRandomOptie]];
}

});
[NSThread sleepForTimeInterval:0.05];
}
});
}

if (showAllert) {
[self showDurationAlert];
}
}

亲切的问候!

最佳答案

使用 dispatch_group_create 创建组。

使用 dispatch_group_async 代替 dispatch_async

你的路径 block ,应该在 foreach 之后执行,并在 dispatch_group_notify 中分派(dispatch)。

一切都很简单。

关于ios - 在 foreach 和 dispatch 之后执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23287881/

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