gpt4 book ai didi

ios - NSOperation和NSOperationQueue iOS的后台任务

转载 作者:行者123 更新时间:2023-12-01 16:56:27 25 4
gpt4 key购买 nike

我想上传一些文件,即使应用程序进入后台,该文件也应继续。

目前,我正在从数据库中检索文件,而不是通过NSOperation将其添加到队列中,然后开始上载过程。

即使应用程序进入后台或前台,也应上传所有文件。
下面是单个任务的代码,任何人都可以给我提示我们如何使它能够用于上传许多文件。

UIApplication* application = [UIApplication sharedApplication];
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
// Clean up any unfinished task business by marking where you
// stopped or ending the task outright.

[application endBackgroundTask: bgTask];
bgTask = UIBackgroundTaskInvalid;
}];

最佳答案

为什么不尝试这样做呢? ..我尚未尝试过,尝试后将发布更新

UIApplication* application = [UIApplication sharedApplication];
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
// Clean up any unfinished task business by marking where you
// stopped or ending the task outright.

// Bring up your NSOperation queue instance here and block this thread until it is complete
[queue waitUntilAllOperationsAreFinished];

[application endBackgroundTask: bgTask];
bgTask = UIBackgroundTaskInvalid;
}];

还应确保您有办法取消所有这些长期存在的后台操作
 bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
// Clean up any unfinished task business by marking where you.
// stopped or ending the task outright.
[queue cancelAllOperations];

[application endBackgroundTask:endSessionTask];
bgTask = UIBackgroundTaskInvalid;
}];

关于ios - NSOperation和NSOperationQueue iOS的后台任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11376873/

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