简体   繁体   中英

Delay inside Background Fetch EXEC_BAD_ACCESS

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?

You can't run forever in the background, iOS just doesn't offer you that ability.

The crash currently is because you're calling the completion handler multiple times and after it has been invalidated.

Basically, you should change your approach, and perhaps your requirement, and use the background processing as prescribed by Apple (so based on user usage rather than explicit time periods).

Wain is correct. Your approach is basically the opposite of Apple vision about background and services.

Would be better is server makes the work and eventually send a push (silent) notification.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM