2016-10-10 46 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