【问题标题】:Fast way of finding scheduled notifications查找预定通知的快速方法
【发布时间】:2016-06-19 13:15:56
【问题描述】:

有没有一种快速有效的方法来查找预定通知? Find list of Local Notification the app has already set 讨论遍历列表,这就是我所做的:

        // To avoid duplicate notifications, check whether there are already scehduled notifications with the same fireDate, reminderName and calendar name.
        if let definiteDueDateComponents = reminder.dueDateComponents, definiteDueDate = definiteDueDateComponents.date, definiteScheduledNotifications = UIApplication.sharedApplication().scheduledLocalNotifications {

            for notification in definiteScheduledNotifications {
                if notification.fireDate?.compare(definiteDueDate) == .OrderedSame {
                    // Check whether there is already a notification set up for definiteDueDate? 
                    // If so, check whether the notification is actually for the same item that I want to set up here.

                }
            }                
        }

但是,这可能会变得低效,特别是如果我有很多项目要在安排它们之前检查(运行上面的代码),而且如果我已经有各种安排的通知。

有没有人尝试过创建预定通知的字典(哈希表)?如果是这样,您何时创建并重新创建它?尝试使哈希表与UIApplication.sharedApplication().scheduledLocalNotifications 保持同步是否很麻烦?

【问题讨论】:

    标签: ios swift uilocalnotification


    【解决方案1】:

    即使您有 10000 条预定通知,您也可能无法检测到用眼睛遍历它们所花费的时间。您必须记录前后的时间并计算差异,并且可能小于 1/1000 秒。

    这是一个“过早优化”的案例。使用 for 循环并完成它。

    如果您有超过 10000 个预定通知,那么您需要重新考虑您的设计。

    【讨论】:

    • 这不仅仅是预定通知的数量。我上面提到的一点是,我可能需要在有 m 个预定通知的同时执行 n 次检查,因此运行时间将为 O(n*m)。另外,为什么你认为它这么快?我执行另一种类型的 I/O:我从代码中的 EKEventStore 获取 1000 个 EKReminder,大约需要 10 秒!绝对引人注目。
    • 这是不同的。计划通知列表是一个内存数组。迭代它会非常快。但是,您需要找到一种避免 O(n*m) 的方法。
    猜你喜欢
    • 1970-01-01
    • 2016-01-14
    • 1970-01-01
    • 1970-01-01
    • 2012-11-19
    • 2010-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多