I am creating an application that works in background mode (minimized), and every 90 seconds it performs a certain method that checks some information on a server. The code is this:
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
...
}
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
MyFristViewController *viewController = (MyFristViewController *)self.window.rootViewController;
[viewController checkData:^(UIBackgroundFetchResult result) {
completionHandler(result);
}];
}
MyFristViewController.m
-(void)checkData:(void (^)(UIBackgroundFetchResult))completionHandler{
...
NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if(error == nil){
completionHandler(UIBackgroundFetchResultNewData);
}else{
completionHandler(UIBackgroundFetchResultNoData);
}
}];
[dataTask resume];
[self performSelector:@selector(checkData:) withObject:nil afterDelay:90.0];
...
}
My code works great, but as I said earlier a command must be run several times every 90 seconds, and for this the end of the method used this command that works preferment:
[self performSelector:@selector(checkData:) withObject:nil afterDelay:90.0];
the problem in this code is that it works perfect the first time, now when he expects 90 seconds and runs the same code a second time, I get the error message EXEC_BAD_ACCESS this line of code:
completionHandler(UIBackgroundFetchResultNewData);
Now if I delete this line of code I receive the message and my app works great (without any crashes):
Application delegate received call to -application:performFetchWithCompletionHandler: but the completion handler was never called.
I believe this is not good to receive this message, in short, what I am wanting to do is run this command every 90 seconds forever when the application is minimized. I'm doing it the right way?