后台提取执行FetchWithCompletionHandler不起作用?



我的应用程序希望每 5 秒更新一次服务器有关用户位置的信息,即使应用程序在后台也是如此。我为它实现了后台提取。

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
}

但问题是

  1. 我无法输入performFetchWithCompletionHandler方法,除非我单击调试>模拟后台提取
  2. 我怎样才能实现每 5 秒调用一次performFetchWithCompletionHandler

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

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

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

最新更新