在iOS中长时间间隔后获取背景

时间:2016-04-30 08:59:11

标签: ios objective-c iphone xcode7 background-process

iOS 7或更高版本中的默认后台获取时间间隔为3 min,但我希望在2 hours之后进行后台获取以进行新闻更新。

我尝试了很多方法,比如ie。

  1. 使用NSTimer
  2. Sleep主题
  3. 设置最低background fetch internal
  4. 经过艰苦的努力,结果是

    1. 经过一段时间申请crash
    2. 一段时间后,应用程序进入foreground然后新闻更新
    3. 一段时间后no new data update
    4. 我试过这段代码:

       - (void)applicationDidEnterBackground:(UIApplication *)application
          {    
           __block UIBackgroundTaskIdentifier taskID=[[UIApplication sharedApplication]beginBackgroundTaskWithExpirationHandler:^{
      
             NSLog(@"background task expiration handler called");
             NSLog(@"Task ID is:%lu",(unsigned long)taskID);
      
             [[UIApplication sharedApplication]endBackgroundTask:taskID];
         }];
      
          if(taskID==UIBackgroundTaskInvalid)
          {
              NSLog(@"Failed to start background task");
              return;
          }
          dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
      
              NSLog(@"Start Background task with %f seconds remaining",[[UIApplication sharedApplication]backgroundTimeRemaining ]);
      
              [self download];
               [NSThread sleepForTimeInterval:25];
              NSLog(@"Finishing Background task with %f seconds remaining",[[UIApplication sharedApplication]backgroundTimeRemaining ]);
              [[UIApplication sharedApplication]endBackgroundTask:taskID];
          });
      }
      
      
      -(void)download
      {
          ViewController *viewController = (ViewController *)self.window.rootViewController;
          NSDate *fetchStart=[NSDate date];
          [viewController fetchNewDataWithCompletionHandler:^(UIBackgroundFetchResult result) {
              viewController.i++;
              if(viewController.i<5)
              {
                  [self download];
              }
              NSLog(@"%lu",(unsigned long)result);
          }];
      
          NSDate *fetchEnd = [NSDate date];
          NSTimeInterval timeElapsed = [fetchEnd timeIntervalSinceDate:fetchStart];
          NSLog(@"Background Fetch Duration: %f seconds", timeElapsed);
      
      
      }
      
        

      我使用recursion更新了五个新闻频道。我的设备没有   已连接到X-Code。我的后台获取功能已启用。

0 个答案:

没有答案