后台获取似乎不会触发 [英] Background Fetch Does Not Appear to Fire

查看:70
本文介绍了后台获取似乎不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我执行了以下列出的操作,并在应用程序提取例程中添加了计数器以突出显示 iOS 8.1 调用提取的次数.

In my app, I have performed the following listed below and have added counter to the app fetch routine to highlight the number of times fetch is called by iOS 8.1.

  1. 打开后台模式并启用后台获取.
  2. 为performFetchWithCompletionHandler"编写代码.NSLog 消息指示获取过程的开始和结束.计数器位于这些消息之间.
  3. 在didFinishLaunchingWithOptions"中添加了代码.但是,我没有使用setMinimumBackgroundFetchInterval",而是使用 60 的两倍(假设秒数).

当我通过设置模拟后台获取"在 Debug 中测试代码时,一切都按预期完美运行,绝对没有问题.计数器工作并显示预期值.

When I test the code in Debug by setting "Simulate Background Fetch" all works perfectly as expected with absolutely no problems. Counters work and show expected values.

然而,当我在 iPhone 上上线时,启动应用程序,然后点击主页按钮进入后台,等待一两个小时或一夜之间.什么也没有发生,没有获取,没有下载,所有的计数器都保持为零.

However, when I go live on the iPhone, launch the app, then hit the home button to put in background, wait one or two hours or overnight. Nothing happens, no fetch, no downloads and all counters remain at zero.

如果我不能让它工作,我将需要创建我自己的后台线程并直接管理它,我不想这样做.

If I cannot get this to work, I will need to create my own background thread and manage it directly, which I would prefer not to do.

非常感谢任何输入或想法.

Any input or ideas are deeply appreciated.

推荐答案

从 iOS 8 开始,我的两个应用都开始遇到同样的问题.在 iOS 7 中,后台刷新触发非常可靠.在 iOS 8 中,它刚刚停止.如果我从 xcode 启动任一应用程序进入后台获取模式,一切都会正常工作.后台刷新本身只是在 iOS 中停止自行触发.我现在正在尝试一个充满希望的理论.这是我的想法...

As of iOS 8, I started having this same problem with both of my apps. In iOS 7, background refresh triggered pretty reliably. In iOS 8, it just stopped. If I launch either app from xcode into the background fetch mode, everything works like it should. Background refreshes themselves just stopped triggering on their own in iOS. I have a hopeful theory that I'm trying out right now. Here's my thinking...

如果用户强制从多任务屏幕中杀死您的应用,iOS 会将您的应用排除在后台刷新之外.Apple 是如何实现这种行为的?一种方法是将您的最小获取间隔设置为 UIApplicationBackgroundFetchIntervalNever.十分简单.让我们假设他们就是这样做的.您的应用是在每次启动时设置最小获取间隔,还是仅在首次启动时设置?

iOS will exclude your app from background refresh if the user force kills it from the multi-tasking screen. How might Apple have implemented this behavior? One way would be to just set your minimum fetch interval to UIApplicationBackgroundFetchIntervalNever. Easy peasy. Let's assume that's how they do it. Does your app set the minimum fetch interval every time it launches, or does it only set it on the initial launch?

就我而言,我只是将最小获取间隔设置为应用程序初始设置的一部分.如果用户强制杀死了应用程序,而 iOS 实际上将最小获取间隔设置为 UIApplicationBackgroundFetchIntervalNever,那么我的应用程序就会陷入从不的状态.我在我的一个应用程序中做了一个小改动,以便它在每次启动时设置最小获取间隔.到目前为止一切顺利.

In my case, I was only setting the minimum fetch interval as part of the initial setup of my app. If the user force killed the app, and iOS is in fact setting the minimum fetch interval to UIApplicationBackgroundFetchIntervalNever, then my apps are stuck in the state of never. I made a minor change in one of my apps, so that it sets the minimum fetch interval on every launch. So far so good.

更新:

在我的后台获取土地上一切都很好.

All is well in my background fetch land.

我当前正在开发的应用程序和我的应用程序位于 应用商店 再次可靠地触发后台提取.

Both my app that's currently in development and my app that's in the app store are once again triggering background fetches reliably.

执行此操作的代码可以非常简单...

The code for doing this can be pretty simple...

[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:minimumBackgroundFetchInterval];

每次您的应用程序启动时调用它都可以解决问题,但是,您需要考虑设置该值是否适合您应用程序的当前状态.就我而言,我的一个应用程序是音乐会列表应用程序.如果用户尚未选择音乐会的位置,则设置最小后台获取间隔没有意义.我有一个 NSUserDefault 来跟踪是否已设置位置.这是我用于设置获取间隔的代码的近似值...

Calling that every time your application launches will do the trick, however, you'll want to consider whether setting that value is appropriate to the current state of your app. In my case, one of my apps is a concert listings app. There's no point in setting a minimum background fetch interval if the user hasn't selected a location for concerts yet. I have an NSUserDefault to track whether a location has been set. Here's an approximation of my code for setting the fetch interval ...

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if ([INCUserDefaults isLocationConfigured]) {
        [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:kSecondsIntervalForUpcomingShowsBackgroundFetch];
    }

    return YES;
}

在我发布错误修复之前,我只是在设置过程中设置了最小后台提取间隔.现在我在设置过程中设置它,在应用程序启动时设置,如果设置过程之前已经完成.

Prior to the bug fix that I issued, I was only setting the minimum background fetch interval as part of the setup process. Now I set it during the setup process, and on application startup if the setup process was previously completed.

这篇关于后台获取似乎不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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