gpt4 book ai didi

当后台服务调用我的应用程序挂起时的 IOS

转载 作者:行者123 更新时间:2023-11-28 21:45:46 25 4
gpt4 key购买 nike

以下是我用来调用 5 个不同服务以获取响应并将其存储在我的 json 文本文件中的代码。当后台服务调用运行时,我的应用程序挂起,没有触摸事件在工作。完成后后台服务调用它的工作。我不想在调用后台服务时挂起我的应用程序,我该怎么做。

[NSTimer scheduledTimerWithTimeInterval:60.0 target:self
selector:@selector(checkAlert) userInfo:nil repeats:YES];

最佳答案

你可以使用 Grand Central Dispatch (GCD),你可以编写代码在后台线程上做一些处理,然后在主运行循环中对结果做一些事情,这非常简单和紧凑:

dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Add code here to do background processing
//
//
dispatch_async( dispatch_get_main_queue(), ^{
// Add code here to update the UI/send notifications based on the
// results of the background processing
});
});

如果想加延迟也可以用

// Delay execution of my block for 10 seconds.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
// call your method
});

关于当后台服务调用我的应用程序挂起时的 IOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30234568/

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