【问题标题】:performFetchWithCompletionHandler not getting called on fixed time intervalperformFetchWithCompletionHandler 没有在固定的时间间隔上被调用
【发布时间】:2023-03-27 05:05:01
【问题描述】:

我正在尝试使用 ios7 的后台获取功能调用 performFecthWithCompletionHandler。 为此,我设置了 180 秒的时间间隔。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    NSLog(@"Did finish launching");
    NSTimeInterval timeInterval = 180;
    [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:timeInterval];
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    return YES;
}

我已经经历了许多线程,它建议 Xcode -> Debug -> Simulate Background fetch。哪个工作正常。

但我希望应用程序在 timeInterval 完成后自动调用此方法。 我尝试在不连接到 Xcode 的情况下运行应用程序。 但它不会在 180 秒后调用。调用它需要更多时间。

-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  NSLog(@"########### Received Backgroudn Fetch ###########");

    //Increase Badge Number
    [UIApplication sharedApplication].applicationIconBadgeNumber++;

    UINavigationController *navigationController = (UINavigationController*)self.window.rootViewController;
    NSLog(@"fetch");
    id topViewController = navigationController.topViewController;
    if ([topViewController isKindOfClass:[ViewController class]]) {
        [(ViewController*)topViewController insertNewObjectForFetchWithCompletionHandler:completionHandler];
    } else {
        NSLog(@"Not the right class %@.", [topViewController class]);
        completionHandler(UIBackgroundFetchResultFailed);
    }
}

我在这里缺少什么吗?或者是什么让它没有被调用

【问题讨论】:

    标签: objective-c iphone ios7 background


    【解决方案1】:

    您无法控制操作系统何时允许您的应用程序在后台“唤醒”并执行后台提取。操作系统根据许多不同的因素决定应允许您的应用程序执行提取的频率。我发现有时具有相同应用时间的不同设备之间存在差异,具体取决于应用在设备上的运行时间、已完成的后台提取次数以及完成它们的速度/效率等。

    当您设置最小后台提取时间间隔时,它不会告诉操作系统执行提取的频率,它告诉操作系统它不应该执行后台获取比这更频繁。最小后台抓取时间间隔:

    指定之间必须经过的最短时间 后台获取操作。

    https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/setMinimumBackgroundFetchInterval:

    我强烈建议您阅读文档以及与 iOS7 多任务相关的任何内容,以更好地了解其工作原理。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-04
      • 1970-01-01
      • 1970-01-01
      • 2019-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多