0

Is there a fast and efficient way of finding scheduled notifications? Find list of Local Notification the app has already set discusses going through the list, which is what I did:

        // 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.

                }
            }                
        }

This, however, may become inefficient, especially if I have many items that I want to check against (run the above code) before I schedule them and also if I already have various scheduled notifications.

Has anybody experimented with creating a dictionary (hash table) of scheduled notifications? If so, when do you create it and recreate it? Is it cumbersome trying to keep the hash table in synch with UIApplication.sharedApplication().scheduledLocalNotifications?

Community
  • 1
  • 1
Daniel
  • 3,758
  • 3
  • 22
  • 43

1 Answers1

-1

Even if you have 10000 scheduled notifications you would probably not be able to detect the amount of time it took to iterate through them with your eyes. You'd have to log the time before and after and calculate the difference, and it would likely be less than 1/1000 of a second.

This is a case of "premature optimization". Use a for loop and be done with it.

If you have more than 10000 scheduled notifications then you need to rethink your design.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • It's not just about the number of scheduled notifications. The point I made above is that I may need to perform the check n times while having m scheduled notifications hence the running time will be O(n*m). Also, Why do you think it's so fast? I perform another type of I/O: I fetch 1000 EKReminders from the EKEventStore in my code and it takes about 10 seconds! Definitely noticeable. – Daniel Jun 19 '16 at 14:15
  • This is different. The list of scheduled notifications is an in-memory array. Iterating it will be very fast. You need to find a way to avoid O(n*m) however. – Duncan C Jun 20 '16 at 03:36