APP在后台完成耗时任务

在后台完成耗时任务

程序从前台退到后台(按home)键后,将执行程序的委托方法 - (void)applicationDidEnterBackground:(UIApplication *)application,这时候,应用程序只给了我们可怜的一点点时间(也就是秒级别的)来处理东西,然后,所有的线程都被挂起了。

而实际中,我们可能需要更长的时间来完成我们的需要的必要操作:
1.我们需要在应用程序推到后台时,能够有足够的时间来完成将数据保存到远程服务器的操作。
2.有足够的时间记录一些需要的信息操作。
怎么办?!因为我们需要的时间可能会有点长,而默认情况下,iOS没有留给我们足够的时间。
如果你想在后台完成一个长期任务,就必须调用 UIApplication 的 beginBackgroundTaskWithExpirationHandler:实例方法,来向 iOS 借点时间。
默认情况下,如果在这个期限内,长期任务没有被完成,iOS 将终止程序,iOS 7之后,这个最长时间是3分钟左右,之前是10分钟左右,这个时间足够我们做绝大多数的业务了。
代码是程序员最好的导师,直接上代码:

//一个后台任务标识符
@property (nonatomic, unsafe_unretained) UIBackgroundTaskIdentifier backgroundTaskIdentifier;
//模拟长期任务
@property (nonatomic, strong) NSTimer *myTimer;
//标记长期任务的时间
@property (nonatomic, assign) NSInteger backgroundTime;

  • (void)applicationDidEnterBackground:(UIApplication )application {
        /
    * 申请一个后台的长期任务*/
        self.backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^(void) {
            //如果系统觉得我们运行时间太长,将执行这个程序块,并停止运行应用程序(iOS8开始之后是3分钟左右,之前是10分钟左右)
            [self endBackgroundTask];
        }];
        self.backgroundTime = 8000;
        // 模拟一个Long-Running Task
        self.myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerMethod:) userInfo:nil repeats:YES];
    }

  • (void)applicationWillEnterForeground:(UIApplication *)application {
        [self endBackgroundTask];
    }

  • (void) endBackgroundTask{ 
        dispatch_queue_t mainQueue = dispatch_get_main_queue(); 
        AppDelegate *weakSelf = self;
        dispatch_async(mainQueue, ^(void) {
            AppDelegate *strongSelf = weakSelf;     
            if (strongSelf != nil){            
                [strongSelf.myTimer invalidate];// 停止定时器
                // 每个对 beginBackgroundTaskWithExpirationHandler:方法的调用,必须要相应的调用 endBackgroundTask:方法。这样,来告诉应用程序你已经执行完成了。
                // 也就是说,我们向 iOS 要更多时间来完成一个任务,那么我们必须告诉 iOS 你什么时候能完成那个任务。
                // 也就是要告诉应用程序:“好借好还”嘛。
                // 标记指定的后台任务完成
                if (strongSelf.backgroundTaskIdentifier) {
                    [[UIApplication sharedApplication] endBackgroundTask:strongSelf.backgroundTaskIdentifier];
                    // 销毁后台任务标识符
                    strongSelf.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
                }
            }
        });
    }

// 模拟的一个 Long-Running Task 方法

  • (void)timerMethod:(NSTimer *)paramSender{   
        self.backgroundTime --;
        if (self.backgroundTime == 1){
            NSLog(@“Background Time Remaining = Undetermined”);
            [self.myTimer invalidate];// 停止定时器
            // 每个对 beginBackgroundTaskWithExpirationHandler:方法的调用,必须要相应的调用 endBackgroundTask:方法。这样,来告诉应用程序你已经执行完成了。
            // 也就是说,我们向 iOS 要更多时间来完成一个任务,那么我们必须告诉 iOS 你什么时候能完成那个任务。
            // 也就是要告诉应用程序:“好借好还”嘛。
            // 标记指定的后台任务完成
            [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskIdentifier];
            // 销毁后台任务标识符
            self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
        } else {
            NSLog(@“Background Time Remaining = %ld Seconds”, (long)self.backgroundTime);
        }
    }
    注意:在任务完成或者重新进入前台时,最好调用endBackgroundTask,遵循好借好还的规则。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值