Swiftコードに関数scheduleFutureLocalNotifications()
があり、将来的に64 UILocalNotification
が生成されます。UILocalNotificationをスケジューリングしてユーザーインターフェイスの遅れと遅延を引き起こす
元は、この関数はviewDidLoad()
で呼び出されましたが、これはアプリケーションの起動時に遅延を引き起こしました。
次に、アクティブなアプリケーションの間に関数が呼び出されましたが、ユーザーインターフェイスが予期しない一時停止または遅れが発生しました。
最後に、通知がUIApplicationDidEnterBackground
の通知を受信した後にアプリがバックグラウンドに移行すると、この機能が起動されましたが、iOSはローカル通知がバックグラウンドで準備されているために一時的に遅れます。これは古いデバイスでより顕著に現れます。
質問:
1 - どのように私は遅れを削減し、ローカル通知を作成するユーザーインターフェースの応答 を向上させることができますか?
2 - どのような優れた手法を使用して通知をスケジュールすることができますか?62
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)
}
}
}
は 'dispatch_async' – Paulw11
にscheduleFutureLocalNotifications''への呼び出しをラップすることにより、バックグラウンドスレッド上でコードを派遣しますありがとう@ Paulw11あなたはさらに詳しく説明できますか?次のことを提案していますか? dispatch_async(DISPATCH_QUEUE_PRIORITY_BACKGROUND、DISPATCH_QUEUE_PRIORITY_BACKGROUND、0)、self.scheduleFutureLocalNotifications()}内の{()}) – user4806509
そして、それが正確に何かについてもう少し説明できますか? – user4806509