gpt4 book ai didi

ios - 如何在 Objective-C 中的特定时间后停止执行方法?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:00:00 26 4
gpt4 key购买 nike

如何在 iOS (Objective-C) 下实现此逻辑?

  [obj method1]

[OCRlib imageScan] // run <10 sec. or should be stopped.
// Display progress bar during execution.
// If does not finish in 10 seconds - stop it.
// OCRlib - third-party code I can't change

[obj method2] // wait imageScan results here

我在这里看到了 Java/C# 的答案,但没有看到 Objective-C..

最佳答案

我不确定如何停止 imageScan 方法,OCRlib 是否提供了实现该效果的方法?

要达到超时,您可以使用调度组

[obj method1]

dispatch_group_t group = dispatch_group_create();

dispatch_group_async(group, queue, ^{
[OCRlib imageScan]
});

dispatch_time_t when = dispatch_time(DISPATCH_TIME_NOW,NSEC_PER_SEC * timeout);
long status = dispatch_group_wait(group, when );

if (status != 0) {
// block completed
[obj method2]
}
else {
// block failed to complete in timeout
}

关于ios - 如何在 Objective-C 中的特定时间后停止执行方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25291117/

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