2016-10-10 5 views
0

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) 
     } 
    } 
} 
+1

は 'dispatch_async' – Paulw11

+0

にscheduleFutureLocalNotifications''への呼び出しをラップすることにより、バックグラウンドスレッド上でコードを派遣しますありがとう@ Paulw11あなたはさらに詳しく説明できますか?次のことを提案していますか? dispatch_async(DISPATCH_QUEUE_PRIORITY_BACKGROUND、DISPATCH_QUEUE_PRIORITY_BACKGROUND、0)、self.scheduleFutureLocalNotifications()}内の{()}) – user4806509

+0

そして、それが正確に何かについてもう少し説明できますか? – user4806509

答えて

1

あなたは別のキューに、非同期的に、機能を派遣することができます。これは、スケジューリングを行うメインキューがブロックされていないことを確認し、応答しなくなってきてからUIを防ぐことができます:

override func viewDidLoad() { 
    super.viewDidLoad() 

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

これは有望だと思う、私はそれを行ってあげるよ。ありがとうございました。 – user4806509

+0

もう1つの質問ですが、私が知っておくべきディスパッチを使用して落とし穴がありますか?ありがとう。 – user4806509

+1

注意しなければならない主なことは、UIアップデートがメインキューにディスパッチされていることを確認することです。これはこのコードの問題ではありませんが、たとえば、テキストフィールドを更新する場合は、メインのキューに戻す必要があります – Paulw11

関連する問題