FetchWithCompletionHandler无法正常工作

FetchWithCompletionHandler无法正常工作

本文介绍了后台获取performFetchWithCompletionHandler无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使应用程序处于后台,我的应用程序也希望每5秒更新一次服务器有关用户位置的信息.我为此实现了背景提取.

  func应用程序(应用程序:UIApplication,didFinishLaunchingWithOptions launchOptions:[NSObject:AnyObject]?)->布尔{application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes:[.Alert,.Badge,.Sound],类别:nil))application.setMinimumBackgroundFetchInterval(5.0)返回真}func application(application:UIApplication,performFetchWithCompletionHandlercompleteHandler:(UIBackgroundFetchResult)->无效){completeHandler(UIBackgroundFetchResult.NewData)UpdateData()}func UpdateData(){打印("UpdateData执行")//此方法触发时用于更新数据的函数} 

但问题是

  1. 除非单击调试>模拟后台提取
  2. ,否则我将无法输入 performFetchWithCompletionHandler 方法
  3. 如何实现每5秒调用一次 performFetchWithCompletionHandler .
解决方案

您对什么是后台获取有误解.开发人员无权无法确定何时准确执行后台抓取.iOS本身会决定何时允许应用执行后台抓取.

setMinimumBackgroundFetchInterval 函数仅允许您指定在后台抓取之间必须经过的 minimum 时间间隔,以最大程度地减少应用程序的能源和数据使用量.但是,您在此处设置的间隔完全不能保证您的应用程序能够频繁执行后台获取.文档中的关键句子在后台机会获取内容...".

目前,确保您的应用程序可以在后台执行某些功能(包括从服务器获取数据)的唯一方法是通过定期发送来自您自己服务器的静默推送通知.但是,即使这样,如果您的应用程序需要很长时间才能完成对响应的执行,或者如果您的应用程序接收到过多的通知,系统仍可能决定不唤醒您的应用程序以响应静默的推送通知.

My app want to update server about users location after every 5 seconds even if app is in background. I implemented Background fetch for it.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil))

        application.setMinimumBackgroundFetchInterval(5.0)

        return true
    }

func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {

    completionHandler(UIBackgroundFetchResult.NewData)
    UpdateData()
}

func UpdateData(){
     print("UpdateData executes")
    // function to update data when ever this method triggers
}

but the problem is

  1. I am unable to enter performFetchWithCompletionHandler method unless I click Debug>simulate background fetch
  2. How can I achieve to invoke performFetchWithCompletionHandler after every 5 seconds.

You have a misunderstanding about what background fetch is. Developers have no power over when a background fetch will be performed exactly. iOS itself decides when to allow an app to perform a background fetch.

The setMinimumBackgroundFetchInterval function only allows you to specify a minimum time interval that has to pass between background fetches to minimize the energy and data usage of your app. However, the interval you set here does not guarantee at all that your app will be able to perform a background fetch this frequently. The key sentence in the documentation regarding this is "Fetch content opportunistically in the background...".

At the moment, the only way to ensure that your app can execute certain functions (including fetching data from a server) in the background is by sending silent push notifications from your own server at regular intervals. However, even then, the system might decide not to wake up your app in response to a silent push notification if your app takes to long to finish execution in response to the push or if your app receives too many notifications.

这篇关于后台获取performFetchWithCompletionHandler无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 22:54