BGTask.m 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // BGTask.m
  3. // locationdemo
  4. //
  5. // Created by yebaojia on 16/2/24.
  6. // Copyright © 2016年 mjia. All rights reserved.
  7. //
  8. #import "BGTask.h"
  9. @interface BGTask()
  10. @property (nonatomic, strong)NSMutableArray* bgTaskIdList; //后台任务数组
  11. @property (assign) UIBackgroundTaskIdentifier masterTaskId; //当前后台任务id
  12. @end
  13. @implementation BGTask
  14. //初始化
  15. +(instancetype)shareBGTask
  16. {
  17. static BGTask *task;
  18. static dispatch_once_t predicate;
  19. dispatch_once(&predicate, ^{
  20. task = [[BGTask alloc]init];
  21. });
  22. return task;
  23. }
  24. -(instancetype)init
  25. {
  26. if (self == [super init]) {
  27. _bgTaskIdList = [NSMutableArray array];
  28. _masterTaskId = UIBackgroundTaskInvalid;
  29. }
  30. return self;
  31. }
  32. //开启新的后台任务
  33. -(UIBackgroundTaskIdentifier)beginNewBackgroundTask
  34. {
  35. UIApplication *application = [UIApplication sharedApplication];
  36. __block UIBackgroundTaskIdentifier bgTaskId = UIBackgroundTaskInvalid;
  37. if([application respondsToSelector:@selector(beginBackgroundTaskWithExpirationHandler:)])
  38. {
  39. bgTaskId = [application beginBackgroundTaskWithExpirationHandler:^{
  40. NSLog(@"bgTask 过期 %lu",(unsigned long)bgTaskId);
  41. [self.bgTaskIdList removeObject:@(bgTaskId)];//过期任务从后台数组删除
  42. bgTaskId = UIBackgroundTaskInvalid;
  43. [application endBackgroundTask:bgTaskId];
  44. }];
  45. }
  46. //如果上次记录的后台任务已经失效了,就记录最新的任务为主任务
  47. if (_masterTaskId == UIBackgroundTaskInvalid) {
  48. self.masterTaskId = bgTaskId;
  49. NSLog(@"开启后台任务 %lu",(unsigned long)bgTaskId);
  50. }
  51. else //如果上次开启的后台任务还未结束,就提前关闭了,使用最新的后台任务
  52. {
  53. //add this id to our list
  54. NSLog(@"保持后台任务 %lu", (unsigned long)bgTaskId);
  55. // [self.bgTaskIdList addObject:@(bgTaskId)];
  56. // [self endBackGroundTask:NO];//留下最新创建的后台任务
  57. }
  58. return bgTaskId;
  59. }
  60. /**
  61. *
  62. @param all : yes 关闭所有 ,no 只留下主后台任务
  63. all:yes 为了去处多余残留的后台任务,只保留最新的创建的
  64. *
  65. **/
  66. -(void)endBackGroundTask:(BOOL)all
  67. {
  68. UIApplication *application = [UIApplication sharedApplication];
  69. //如果为all 清空后台任务数组
  70. //不为all 留下数组最后一个后台任务,也就是最新开启的任务
  71. if ([application respondsToSelector:@selector(endBackGroundTask:)]) {
  72. for (int i = 0; i < (all ? _bgTaskIdList.count :_bgTaskIdList.count -1); i++) {
  73. UIBackgroundTaskIdentifier bgTaskId = [self.bgTaskIdList[0]integerValue];
  74. NSLog(@"关闭后台任务 %lu",(unsigned long)bgTaskId);
  75. [application endBackgroundTask:bgTaskId];
  76. [self.bgTaskIdList removeObjectAtIndex:0];
  77. }
  78. }
  79. ///如果数组大于0 所有剩下最后一个后台任务正在跑
  80. if(self.bgTaskIdList.count > 0)
  81. {
  82. NSLog(@"后台任务正在保持运行 %ld",(long)[_bgTaskIdList[0]integerValue]);
  83. }
  84. if(all)
  85. {
  86. [application endBackgroundTask:self.masterTaskId];
  87. self.masterTaskId = UIBackgroundTaskInvalid;
  88. }
  89. else
  90. {
  91. NSLog(@"kept master background task id %lu", (unsigned long)self.masterTaskId);
  92. }
  93. }
  94. @end