簡體   English   中英

后台獲取performFetchWithCompletionHandler無法正常工作?

[英]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
}

但問題是

  1. 我無法輸入performFetchWithCompletionHandler方法,除非單擊“ 調試”>“模擬后台提取”
  2. 如何實現每5秒調用一次performFetchWithCompletionHandler

您對什么是背景獲取有誤解。 當准確執行后台獲取時,開發人員無法控制 iOS本身會決定何時允許應用執行后台獲取。

setMinimumBackgroundFetchInterval函數僅允許您指定后台抓取之間必須經過的最小時間間隔,以最大程度地減少應用程序的能源和數據使用量。 但是,您在此處設置的間隔完全不能保證您的應用程序能夠頻繁執行后台獲取。 文檔中與此相關的關鍵句子是“在后台機會獲取內容...”

目前,確保您的應用程序可以在后台執行某些功能(包括從服務器獲取數據)的唯一方法是通過定期從您自己的服務器發送靜默推送通知。 但是,即使這樣,如果您的應用程序需要很長時間才能完成對響應的響應,或者如果您的應用程序接收到過多的通知,系統仍可能決定不喚醒您的應用程序以響應無提示的推送通知。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM