gpt4 book ai didi

ios - dispatch_async超时方法调用

转载 作者:行者123 更新时间:2023-12-01 17:39:18 27 4
gpt4 key购买 nike

在设定的时间限制后,是否有一种好的方法可以调用外部方法来完成下面概述的长过程?我希望漫长的过程在设置的时间间隔后停止尝试,并调用方法尝试其他操作并包装请求。

dispatch_async(dispatch_get_global_queue(0, 0), ^{

//// LONG PROCESS

dispatch_async(dispatch_get_main_queue(), ^{

//// RESULTS PROCESS

});
});

最佳答案

为了“杀死”正在运行的程序块,您必须检查条件。这将允许您进行清理。请考虑以下修改:

dispatch_async(dispatch_get_global_queue(0, 0), ^{

BOOL finished = NO;
__block BOOL cancelled = NO;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
if (!finished) {
cancelled = YES;
}
});

void (^cleanup)() = ^{
// CLEANUP
};

//// LONG PROCESS PORTION #1
if (cancelled) {
cleanup();
return;
}

//// LONG PROCESS PORTION #2
if (cancelled) {
cleanup();
return;
}

// etc.

finished = YES;

dispatch_async(dispatch_get_main_queue(), ^{

//// RESULTS PROCESS

});
});

关于ios - dispatch_async超时方法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26873239/

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