持续设置新的本地通知-ios

持续设置新的本地通知-ios,ios,uilocalnotification,Ios,Uilocalnotification,我正在做模拟警报的工作 为此,我尝试在某些条件为真时每隔X秒发送一次新的本地通知,在某些条件为假时停止(当用户取消报警时) 我有一个Set按钮和一个Stop按钮 以下代码用于按下设置按钮时: @IBAction func setNotification(_ sender: Any) { // timeEntered = time.text! // let hrMin = timeEntered.components(separatedBy: ":");

我正在做模拟警报的工作

为此,我尝试在某些条件为真时每隔X秒发送一次新的本地通知,在某些条件为假时停止(当用户取消报警时)

我有一个
Set
按钮和一个
Stop
按钮

以下代码用于按下
设置
按钮时:

@IBAction func setNotification(_ sender: Any) {
//        timeEntered = time.text!
//        let hrMin = timeEntered.components(separatedBy: ":");
        let content = UNMutableNotificationContent()
        content.title = "How many days are there in one year"
        content.subtitle = "Do you know?"
        content.body = "Do you really know?"
        content.badge = 1
        content.sound = UNNotificationSound(named: "alarm.aiff");

        while (repeatNotifications) {
            trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false);

            let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)

            UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
        }
}
@IBAction func stopRepeat(_ sender: Any) {
    repeatNotifications = false
    UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
    UNUserNotificationCenter.current().removeAllDeliveredNotifications()
}
按下
停止
按钮时:

@IBAction func setNotification(_ sender: Any) {
//        timeEntered = time.text!
//        let hrMin = timeEntered.components(separatedBy: ":");
        let content = UNMutableNotificationContent()
        content.title = "How many days are there in one year"
        content.subtitle = "Do you know?"
        content.body = "Do you really know?"
        content.badge = 1
        content.sound = UNNotificationSound(named: "alarm.aiff");

        while (repeatNotifications) {
            trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false);

            let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)

            UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
        }
}
@IBAction func stopRepeat(_ sender: Any) {
    repeatNotifications = false
    UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
    UNUserNotificationCenter.current().removeAllDeliveredNotifications()
}
现在,当我按下
Set
按钮时,它被卡在按下模式(颜色变化),除了home按钮外,我不能按任何其他按钮(即:
Stop
按钮)。此外,未设置任何本地通知

有没有办法做到这一点


tl;dr:如何每X秒设置一次新的本地通知,直到按下某个按钮,而您的情况运行得比您想象的要快。它可能通过创建大量对象阻塞了通知类。请使用计时器每5秒运行一次以推送通知。
您可以使用仪器工具查看问题

您的while状态比您想象的运行得更快。它可能通过创建大量对象阻塞了通知类。请使用计时器每5秒运行一次以推送通知。
您可以使用仪器工具查看问题

您可能需要提到您希望通过此功能实现什么?你想开发一个报警应用程序吗?@MDAslamAnsari,我已经把它添加到问题的开头了!您可能需要提到您希望通过此功能实现什么?你想开发一个报警应用程序吗?@MDAslamAnsari,我已经把它添加到问题的开头了!您可以在此处检查计时器的工作方式:和:我尝试了您的建议,但出现了以下问题:您可以在此处检查计时器的工作方式:和:我尝试了您的建议,但出现了以下问题: