title: Long-term background task execution on iOS 6
body: <p>I'm creating an online-shop-style app where users can browse different products on their iPad and order these products. The ordering process consists of creating an xml-file with the user's data and the relevant products he would like to order. But sometimes there might be the case, that users don't have an internet connection right now and I would like to create some mechanism, which checks every x minutes for an active internet connection and then tries to deliver the order-xml. It should repeat this step until it gets connected to the web and then just stop it, when all offline carts have been sent.</p>
  
  <p>I have already been searching the web but only found ways to do this on iOS 7 (with UIBackgroundModes - fetch). But I don't want to use iOS 7 because the app is already done and I'm not planning to redesign it for iOS 7 (it's an Enterprise App). As far as I know, the current Background Execution time on iOS 6 is limited to something like 15 minutes, is that correct?</p>
  
  <p>Any ideas on how to solve that?</p>
  
  <p>Thanks.</p>
  
  <p>EDIT:
  I have tried the following in <code>- (void)applicationDidEnterBackground:(UIApplication *)application</code></p>
  
  <pre><code>self.queue = [[NSOperationQueue alloc] init];
  [self.queue addOperationWithBlock:^{
      [[InstanceHolder getInstance] startNetworkTimer];
  }];
  </code></pre>
  
  <p>and here is what should happen next:</p>
  
  <pre><code>- (void) startNetworkTimer{
      if ([CommonCode getAllOfflineCartsForClient:nil].count &gt; 0){
          NSTimer *pauseTimer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(offlineCartLoop:) userInfo:nil repeats:YES];
      }
  
  }
  
  - (void) offlineCartLoop:(id)sender{
      if([CommonCode isInternetConnectionAvailable]){
          [self sendOfflineCarts];
          [sender invalidate];
      }
  }
  </code></pre>
  
  <p><code>startNetworkTimer</code> gets called as it should, but then it doesn't call the <code>offlineCartLoop</code> function :-(</p>
  
  <p>EDIT 2:</p>
  
  <p>I think the timer-thing was the problem. I'm now calling the <code>offlineCartLoop</code> function like this: </p>
  
  <pre><code>self.queue = [[NSOperationQueue alloc] init];
  [self.queue addOperationWithBlock:^{
      [[InstanceHolder getInstance] offlineCartLoop:nil];
  }];
  </code></pre>
  
  <p>and changed the <code>offlineCartLoop</code> function to this:</p>
  
  <pre><code>- (void) offlineCartLoop:(id)sender{
      if([CommonCode isInternetConnectionAvailable]){
          [self sendOfflineCarts];
      }else{
          [NSThread sleepForTimeInterval:10.0];
          [self offlineCartLoop:nil];
      }
  }
  </code></pre>
  
  <p>Seems to work, but will this run forever? Is there anything else I need to take care of?</p>