简体   繁体   English

内部提取后台执行EXEC_BAD_ACCESS的延迟

[英]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. 我正在创建一个在后台模式下工作的应用程序(最小化),每隔90秒它就会执行某种检查服务器上某些信息的方法。 The code is this: 代码是这样的:

AppDelegate.m 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 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: 我的代码运行良好,但是正如我之前所说的,命令必须每90秒运行几次,为此,方法的结尾使用了该命令,该命令可以优先执行:

[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: 这段代码中的问题是它第一次可以完美运行,现在,当他期望90秒并第二次运行相同的代码时,我得到了错误消息EXEC_BAD_ACCESS这行代码:

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. 应用程序委托收到了对-application:performFetchWithCompletionHandler:的调用,但从未调用完成处理程序。

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. 我认为这对接收此消息不利,总之,我想做的是在应用程序最小化后,每90秒永远运行一次此命令。 I'm doing it the right way? 我做对了吗?

You can't run forever in the background, iOS just doesn't offer you that ability. 您不能永远在后台运行,iOS却无法提供这种功能。

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). 基本上,您应该更改方法,可能还需要更改,并按照Apple的规定使用后台处理(因此,应基于用户使用情况而不是明确的时间段)。

Wain is correct. 没错是正确的。 Your approach is basically the opposite of Apple vision about background and services. 您的方法基本上与Apple关于背景和服务的愿景相反。

Would be better is server makes the work and eventually send a push (silent) notification. 更好的是服务器进行工作,并最终发送推送(静默)通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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