如何实现任务完成

发布于 2024-10-08 05:03:56 字数 502 浏览 6 评论 0原文

任务完成——应用程序可以要求系统额外的时间来完成给定的任务。

我正在使用此方法来完成任务,

- (void)applicationDidEnterBackground:(UIApplication *)application 
{
    UIApplication  *app = [UIApplication sharedApplication];
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^
    { 
         NSLog(@"This is Testing");

        [app endBackgroundTask:bgTask]; 

        bgTask=UIBackgroundTaskInvalid;
    }];
}

但我没有从该方法中获得任何输出。任何人告诉我,我做错了什么。你能告诉我完成任务的最佳方法吗?

问候,

Arunkumar.P

Task completion — applications can ask the system for extra time to complete a given task.

I am using this method for Task Completion,

- (void)applicationDidEnterBackground:(UIApplication *)application 
{
    UIApplication  *app = [UIApplication sharedApplication];
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^
    { 
         NSLog(@"This is Testing");

        [app endBackgroundTask:bgTask]; 

        bgTask=UIBackgroundTaskInvalid;
    }];
}

But i am not getting any output from this method. Anyone tell me, what i am doing wrong.Can you tell any best method to implement for the task completion.

Regards,

Arunkumar.P

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海螺姑娘 2024-10-15 05:03:57

您正在设置一个过期处理程序,但您似乎并未在后台实际执行任何操作。看起来您上面的代码是从

// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    // Do the work associated with the task.

    [app endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
});

在达到时间限制(10 分钟,我上次检查时)之前,不会调用过期处理程序;您在异步分派的任务中完成工作,而不是在过期处理程序中完成。

You are setting up an expiration handler, but you don't appear to be actually doing anything in the background. It looks like the code you have above is copied from the Executing Code in the Background section of the iOS app programming guide. The next piece of code in that exxample is:

// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    // Do the work associated with the task.

    [app endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
});

The expiration handler won't be called until the time limit (10 minutes, last time I checked) is reached; you do the work in the task that you dispatch asynchronously, not in the expiration handler.

姐不稀罕 2024-10-15 05:03:57

这解决了我的问题:iOS 4 中的简单后台任务

主要思想是调度一个不执行任何操作的异步执行代码,但允许当前代码继续运行。

希望它也能解决你的问题!

This solved my problem: Easy Background Tasks In IOS 4.

The main idea is to dispatch an async execution code that does nothing, but allows your current code to keep running.

Hope it solves yours too!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文