나는 나중에을 생성하는 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)
}
}
}
은'dispatch_async' – Paulw11
에 scheduleFutureLocalNotifications' '에 대한 호출을 배치하여 배경 스레드에 코드를 전달 고마워요 @ Paulw11 더 설명해 주시겠습니까? 당신은 다음을 할 것을 제안합니까? dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), {(void) self.scheduleFutureLocalNotifications()}) – user4806509
그리고 그게 정확히 무엇을하는지 조금 더 설명해 주시겠습니까? – user4806509