2016-10-10 4 views
0

나는 나중에을 생성하는 Swift 코드에 scheduleFutureLocalNotifications() 함수를 가지고 있습니다.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_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), {(void) 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

다른 질문 하나, 알고 있어야 할 파견을 사용하는 함정이 있습니까? 감사. – user4806509

+1

주의해야 할 주요 사항은 모든 UI 업데이트가 기본 대기열에 발송되는지 확인하는 것입니다. 이 코드에서는 문제가되지 않지만 예를 들어 텍스트 필드를 업데이트하는 경우이를 다시 메인 큐로 보내야합니다 – Paulw11