使用Xcode进行模拟时,PerformFetchWithCompletionHandler调用两次 [英] PerformFetchWithCompletionHandler called twice when simulating with Xcode

查看:154
本文介绍了使用Xcode进行模拟时,PerformFetchWithCompletionHandler调用两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Xcode 7.0.1中,simulate backgroundfetch命令导致 performFetchWithCompletionHandler 被触发两次。

In Xcode 7.0.1 the "simulate background" fetch command causes performFetchWithCompletionHandler to be triggered twice.

这是一个Xcode调试错误,还是在运行应用程序发布版本的设备上发生这种情况。

Is this an Xcode debugging error, or can this happen on a device running a release build of the application.

更新
现在我们有Xcode 7.1.1并且仍然会调用 performFetchWithCompletionHandler 两次。由于我不确定这是否也在野外发生,如果我的fetch操作已经在运行,我会保持状态。

Update Now we have Xcode 7.1.1 and still performFetchWithCompletionHandler is called twice. Since I am not sure if this also happens "in the wild" I am keeping a state if my fetch action is already running.

- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler {
    if (self.performingFetch) {
        return completionHandler(UIBackgroundFetchResultNoData);
    }
    self.performingFetch = YES;
    ...
    self.performingFetch = NO;
}


推荐答案

我解决了这个问题在App Delegate中声明一个静态布尔值,然后使用布尔值来执行后台提取执行一次

I got around this issue by declaring a static boolean in the App Delegate, and then using the boolean to get the background fetch to perform once

if (!runOnce)
{
    [submission startSubmissionProcessWithCompletetionHandler:^(UIBackgroundFetchResult result){
        NSDate *fetchStart = [NSDate date];

        completionHandler(result);

        NSDate *fetchEnd = [NSDate date];
        NSTimeInterval timeElapsed = [fetchEnd timeIntervalSinceDate:fetchStart];
        NSLog(@"Background Fetch Duration: %f seconds", timeElapsed);
    }];
    runOnce = YES;
}
else
{
    completionHandler(UIBackgroundFetchResultNoData);
    runOnce = NO;
}

这篇关于使用Xcode进行模拟时,PerformFetchWithCompletionHandler调用两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆