![](/img/trans.png)
[英]When to call completionHandler in application:performFetchWithCompletionHandler: when Background Fetch is async?
[英]Background fetch performFetchWithCompletionHandler not working?
我的應用程序想每隔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
}
但問題是
您對什么是背景獲取有誤解。 當准確執行后台獲取時,開發人員無法控制 。 iOS本身會決定何時允許應用執行后台獲取。
setMinimumBackgroundFetchInterval
函數僅允許您指定后台抓取之間必須經過的最小時間間隔,以最大程度地減少應用程序的能源和數據使用量。 但是,您在此處設置的間隔完全不能保證您的應用程序能夠頻繁執行后台獲取。 文檔中與此相關的關鍵句子是“在后台機會獲取內容...” 。
目前,確保您的應用程序可以在后台執行某些功能(包括從服務器獲取數據)的唯一方法是通過定期從您自己的服務器發送靜默推送通知。 但是,即使這樣,如果您的應用程序需要很長時間才能完成對響應的響應,或者如果您的應用程序接收到過多的通知,系統仍可能決定不喚醒您的應用程序以響應無提示的推送通知。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.