微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

beginBackgroundTaskWithExpirationHandler

直接代码

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    __block uibackgroundtaskIdentifier background_task;
    //注册一个后台任务,告诉系统我们需要向系统借一些事件
    background_task = [application beginBackgroundTaskWithExpirationHandler:^ {
        
        //不管有没有完成,结束background_task任务
        [application endBackgroundTask: background_task];
        background_task = uibackgroundtaskInvalid;
    }];

    /*
     尽量用异步,如果像这样 你会发现主线程会被阻塞
     while(TRUE)
     {
     NSLog(@"Background time Remaining: %f",[[UIApplication sharedApplication] backgroundTimeRemaining]);
     [NSThread sleepForTimeInterval:1]; //wait for 1 sec
     }
     */
    //异步
    dispatch_async(dispatch_get_global_queue(disPATCH_QUEUE_PRIORITY_DEFAULT,0),^{
        
        //### background task starts
        NSLog(@"Running in the background\n");
        
        static int i = 0;
        while(i < 5)
        {
            NSLog(@"Background time Remaining: %f",[[UIApplication sharedApplication] backgroundTimeRemaining]);
            [NSThread sleepForTimeInterval:1]; //wait for 1 sec
            i++;
        }
        /*
         while(TRUE)
         {
         NSLog(@"Background time Remaining: %f",[[UIApplication sharedApplication] backgroundTimeRemaining]);
         [NSThread sleepForTimeInterval:1]; //wait for 1 sec
         i++;
         }
         */
        
        //我们自己完成任务后,结束background_task任务
        //如果我们用while(TRUE)的话,下面这两行代码应该不会执行
        [application endBackgroundTask: background_task];
        background_task = uibackgroundtaskInvalid;
    });
}

Despite its name, beginBackgroundTaskWithExpirationHandler: does not actually "begin" a task. It might be better thought of as "register..." rather than "begin...." You're just telling the system that you're in the middle of doing something that would like to complete if that's ok.

Several points:

  • In almost all cases,you want to call beginBackgroundTaskWithExpirationHandler: when you start doing the thing you want to do,and then endBackgroundTask: when you're done. You almost never call these at the point that the user presses the Home button (unless that's the point when you start the task,such as saving something to the server).

  • You can have as many "background tasks" as you want open at a time. They cost nothing. They're like retain counts. As long as you still have one open (a "begin" that did not get to its "end"),then the system will consider that a request for more time. There is very little cost to calling the "begin" and "end" methods,so use these to bracket anything that you want extra time to finish.

  • There is no way to be certain that you will get to finish your sync. These methods just make a request. The OS may still deny that request. The OS may still kill you anytime it needs some extra resources. The OS is not infinitely patient; if you take too long (about 10 minutes usually),it'll kill you anyway after calling your expiration handler. The OS may kill you because the phone is being turned off. Your app must be able to deal with not getting to finish. The OS just gives you this ability so that you can improve the user experience

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐