PerformFetchWithCompletionHandler called twice when simulating with Xcode

纵然是瞬间 提交于 2019-12-20 10:17:12

问题


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

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

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;
}

回答1:


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;
}


来源:https://stackoverflow.com/questions/33105744/performfetchwithcompletionhandler-called-twice-when-simulating-with-xcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!