performFetchWithCompletionHandler未在固定时间间隔内调用 [英] performFetchWithCompletionHandler not getting called on fixed time interval

查看:837
本文介绍了performFetchWithCompletionHandler未在固定时间间隔内调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to call performFecthWithCompletionHandler using background fetch feature of ios7. For that I have set 180seconds of time interval.

- (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 - >模拟后台提取。哪个工作正常。

I have gone through many threads where it suggests for Xcode -> Debug -> Simulate Background fetch. Which is working fine.

但我希望应用程序在timeInterval完成后自动调用此方法。
我试过在不连接Xcode的情况下运行应用程序。
但它在180秒后没有调用。调用它需要一些时间。

But I want the app to call this method automatically after timeInterval finishes. I have tried with running the app without connecting to Xcode. But it does not call after 180seconds. It takes some more time to getting it called.

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

我在这里缺少什么?或者是什么让它不被称为

Anything I am missing here? Or what makes it not get called

推荐答案

当操作系统允许你的应用程序唤醒时你无法控制在后台并执行后台获取。操作系统根据许多不同的因素决定应该允许您的应用程序执行提取的频率。我已经看到,不同设备之间的这种情况因应用程序的有效时间而异,具体取决于应用程序在设备上的使用时间,已完成的背景提取次数以及完成它们的速度/效率等等。

You do not get to control when the operating system allows your application to "wake up" in the background and perform the background fetch. The OS decides, based on many different factors, how often your application should be allowed to perform the fetch. I've seen this vary between different devices with the same app hours sometimes, depending how long the application has been on the device, how many background fetches it has completed and how quickly/efficiently it completes them, etc.

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

When you set the minimum background fetch time interval, that does not tell the OS how often to perform the fetch, it tells the OS that it SHOULD NOT perform the background fetch more often than that. The minimum background fetch time interval:


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

Specifies the minimum amount of time that must elapse between background fetch operations.

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

我强烈建议您阅读文档以及与iOS7多任务相关的任何内容,以便更好地了解如何这很有效。

I would highly recommend reading the documentation and anything related to iOS7 multitasking to get a better understanding of how this works.

这篇关于performFetchWithCompletionHandler未在固定时间间隔内调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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