2016-10-10 81 views
0

我有一個函數scheduleFutureLocalNotifications()在Swift代碼中創建64 UILocalNotification將在未來觸發。調度導致滯後和延遲用戶界面的UILocalNotification

最初的功能是在viewDidLoad()處調用的,但是這會在啓動應用程序時造成延遲。

接下來,該函數在活動應用程序期間被調用,但是這導致了不可預知的用戶界面的暫停或滯後。

最後,當應用程序在收到UIApplicationDidEnterBackground通知後轉換到背景時,該功能被移動以觸發,但這會導致iOS短暫滯後,因爲本地通知在後臺準備好。這在舊設備上顯得更爲明顯。

問:

1 - 如何減少延遲,提高用戶界面響應 創建本地通知?

2 - 可以採用哪些更好的技術來安排64 通知?

3 - 函數scheduleFutureLocalNotifications()可以調用哪個更好的時間?

代碼:

import UIKit 

class ViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     scheduleFutureLocalNotifications() 
    } 

    func scheduleFutureLocalNotifications() { 

     // Remove all previous local notifications 
     let application = UIApplication.sharedApplication() 
     application.cancelAllLocalNotifications() 

     // Set new local notifications to fire over the coming 64 days 
     for nextLocalNotification in 1...64 { 

      // Add calendar day 
      let addDayComponent = NSDateComponents() 
      addDayComponent.day = nextLocalNotification 
      let calendar = NSCalendar.currentCalendar() 
      let nextDate = calendar.dateByAddingComponents(addDayComponent, toDate: NSDate(), options: []) 

      // Set day components for next fire date 
      let nextLocalNotificationDate = NSCalendar.currentCalendar() 
      let components = nextLocalNotificationDate.components([.Year, .Month, .Day], fromDate: nextDate!) 
      let year = components.year 
      let month = components.month 
      let day = components.day 

      // Set notification fire date 
      let componentsFireDate = NSDateComponents() 
      componentsFireDate.year = year 
      componentsFireDate.month = month 
      componentsFireDate.day = day 
      componentsFireDate.hour = 0 
      componentsFireDate.minute = 0 
      componentsFireDate.second = 5 
      let fireDateLocalNotification = calendar.dateFromComponents(componentsFireDate)! 

      // Schedule local notification 
      let localNotification = UILocalNotification() 
      localNotification.fireDate = fireDateLocalNotification 
      localNotification.alertBody = "" 
      localNotification.alertAction = "" 
      localNotification.timeZone = NSTimeZone.defaultTimeZone() 
      localNotification.repeatInterval = NSCalendarUnit(rawValue: 0) 
      localNotification.applicationIconBadgeNumber = nextLocalNotification 

      application.scheduleLocalNotification(localNotification) 
     } 
    } 
} 
+1

通過在'dispatch_async' – Paulw11

+0

包裹調用'scheduleFutureLocalNotifications'分派代碼到後臺線程謝謝@ Paulw11你能解釋一下嗎?你是否打算做以下事情? dispatch_get_global_queue(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0),{() - > void self.scheduleFutureLocalNotifications()}) – user4806509

+0

你能解釋一下更多關於到底是什麼嗎? – user4806509

回答

1

您可以調度功能,異步,到另一個隊列。這將確保在主隊列不被阻擋地進行調度,並防止變得無響應的用戶界面:

override func viewDidLoad() { 
    super.viewDidLoad() 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIO‌​RITY_BACKGROUND, 0)) { 
     scheduleFutureLocalNotifications() 
    } 
} 
+0

這聽起來很有希望,我會放棄它。謝謝。 – user4806509

+0

還有一個問題,我應該知道使用調度有沒有什麼陷阱?謝謝。 – user4806509

+1

你必須小心的主要事情是確保在主隊列上分派任何UI更新。這不是這個代碼中的問題,但是如果你正在更新文本字段,例如,你需要將它分派回主隊列 – Paulw11