iosobjective-cuibackgroundtask

iOS crash on Starting Background Task


I've recently revisited my app which was last built for iOS 8.4 and now updating it to 9.3. It was working fine the first few times I ran it but now crashes on every open, across each simulator with the error: EXC_BAD_ACCESS(code=50)

-(void)beginBackgroundUpdateTask
{
  if (self.backgroundTaskAgent == UIBackgroundTaskInvalid)
   {
     self.backgroundTaskAgent = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^(void) {
        [self endBackgroundUpdateTask];
     }];
   }
}
-(void)endBackgroundUpdateTask
{
    if (self.backgroundTaskAgent != UIBackgroundTaskInvalid)
    {
        [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskAgent];
        self.backgroundTaskAgent = UIBackgroundTaskInvalid;
    }
}

The crash occurs in the top method with beginBackgroundTaskWithExpirationHandler. I've never had this error before and can't find any solution to it myself or on SO.


Solution

  • Try like this,

     -(void)beginBackgroundUpdateTask
    {
      self.backgroundTaskAgent == UIBackgroundTaskInvalid
    
      self.backgroundTaskAgent = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^(void) {
        [self endBackgroundUpdateTask];
     }];
    
    }
     -(void)endBackgroundUpdateTask
    {
    
        [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskAgent];
        self.backgroundTaskAgent = UIBackgroundTaskInvalid;
    
     }