gpt4 book ai didi

ios - 后台获取 performFetchWithCompletionHandler 不工作?

转载 作者:行者123 更新时间:2023-11-28 10:08:47 25 4
gpt4 key购买 nike

即使应用程序在后台,我的应用程序也希望每 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

最佳答案

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

setMinimumBackgroundFetchInterval 函数仅允许您指定必须在后台提取之间传递的最小 时间间隔,以最大限度地减少应用的能源和数据使用。但是,您在此处设置的间隔根本不能保证您的应用程序能够如此频繁地执行后台提取。 documentation中的关键句关于此的是“在后台机会性地获取内容...”

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

关于ios - 后台获取 performFetchWithCompletionHandler 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50375733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com