2016-02-25 15 views
3

我遵循所有步驟來設置background fetch,但我懷疑我寫了函數performFetchWithCompletionHandler在函數performFetchWithCompletionHandler AppDelegate中。我得到一個警告,說完成處理程序從來沒有被調用當我模擬後臺提取

這裏只要是我得到的警告,因爲我模仿background fetch

Warning: Application delegate received call to - application: 
performFetchWithCompletionHandler:but the completion handler was never called. 

這裏是我的代碼:

func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 
    if let tabBarController = window?.rootViewController as? UITabBarController, 
      viewControllers = tabBarController.viewControllers as [UIViewController]! { 
     for viewController in viewControllers { 
     if let notificationViewController = viewController as? NotificationsViewController { 
     firstViewController.reloadData() 
     completionHandler(.NewData) 
     print("background fetch done") 
     } 
    } 
    } 
} 

如何測試如果background-fetch工作?

回答

2

如果您沒有輸入第一個if語句,則永遠不會調用完成處理程序。另外,當你通過視圖控制器循環時,你可能找不到你要查找的視圖控制器,這意味着完成永遠不會被調用。最後,在調用完成處理程序之後,您應該放一個return

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

    if let tabBarController = window?.rootViewController as? UITabBarController, 
      viewControllers = tabBarController.viewControllers { 

     for viewController in viewControllers { 
      if let notificationViewController = viewController as? NotificationsViewController { 

       notificationViewController.reloadData() 
       completionHandler(.NewData) 
       print("background fetch done") 
       return 
      } 
     } 
    } 
    completionHandler(.Failed) 
} 
+0

但我的viewController總是在那裏,所以理論上它應該總是找到正確的?它應該總是進入If語句中? – AziCode

+1

我會這麼認爲,但警告似乎證明不是。 – AdamPro13

+0

爲了從'performFetchWithCompletionHandler'中調用位於我的一個VC中的函數,需要通過視圖控制器循環嗎? – AziCode

相關問題