ISO实现段时间内后台运行方式
- (void)applicationDidEnterBackground:(UIApplication *)application {
UIApplication* app = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier backgroundTask;
backgroundTask = [app beginBackgroundTaskWithExpirationHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
if(backgroundTask != UIBackgroundTaskInvalid)
{
backgroundTask = UIBackgroundTaskInvalid;
}
});
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
if(backgroundTask != UIBackgroundTaskInvalid)
{
backgroundTask = UIBackgroundTaskInvalid;
});
});
}
用这种方式可以实现app在段时间内不会被iOS挂起, 但一般时间不会超过10分钟
网友评论