How to implement Task completion

拟墨画扇 提交于 2019-11-28 12:00:37

问题


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


回答1:


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.




回答2:


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!



来源:https://stackoverflow.com/questions/4457619/how-to-implement-task-completion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!