【问题标题】:Scheduling UILocalNotification causing lagging and delay of user interface调度 UILocalNotification 导致用户界面滞后和延迟
【发布时间】:2017-02-19 04:54:54
【问题描述】:

我在 Swift 代码中有一个函数 scheduleFutureLocalNotifications(),它创建了 64 个 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)
        }
    }
}

【问题讨论】:

  • 通过将对scheduleFutureLocalNotifications 的调用包装在dispatch_async 中,将代码发送到后台线程
  • 谢谢@Paulw11 你能进一步解释一下吗?您是否建议执行以下操作? dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), {() -> self.scheduleFutureLocalNotifications() 中的无效})
  • 你能解释一下它到底是做什么的吗?

标签: ios swift optimization nsdate uilocalnotification


【解决方案1】:

您可以将函数异步分派到另一个队列。这将确保主队列不会被阻止执行调度并防止 UI 变得无响应:

override func viewDidLoad() {
    super.viewDidLoad()

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

【讨论】:

  • 这听起来很有希望,我会试一试。谢谢。
  • 还有一个问题,我应该注意使用调度的任何陷阱吗?谢谢。
  • 您必须注意的主要事情是确保所有 UI 更新都在主队列上分派。在这段代码中这不是问题,但如果您正在更新文本字段,例如,您需要将其分派回主队列
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-29
  • 2013-09-24
  • 1970-01-01
相关资源
最近更新 更多