micadelli
micadelli

Reputation: 2482

How to schedule multiple local notifications without freezing UI?

In my PhoneGap 1.3 offline app I'm using modified iOS local notification plugin found in official plugin GitHub page.

I need to schedule a repeating notification (weekly/monthly), or multiple single notifications fired on every 2 weeks, based on user defined setting.

The problem occurs when I'm trying to schedule those 26 notifications to be fired on every 2 weeks.

Currently I'm using this to achieve that:

for (int i = 1; i < 26; i++)
{
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];

    if (localNotif == nil) {
        return;
    }

    NSDate *date = [[NSDate date] dateByAddingTimeInterval: i * 60 * 60 * 24 * 7 * 2];

    localNotif.fireDate = date;
    localNotif.timeZone = [NSTimeZone defaultTimeZone];
    localNotif.alertBody = [NSString stringWithFormat:@"%i: %@", i, msg];

    localNotif.hasAction = NO;
    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = 0;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];        

    [localNotif release];

}

When running this on iPhone 3G or iPod Touch 2 the UI freezes for about 3 - 5 seconds which is a big no no. Is there a way to optimize scheduling notifications?

I know that using only weekly and monthly repeating notifications would solve the problem but the client wants every 2 weeks, every 2 months, and every 3 months notifications, so I need to schedule multiple notifications.

I have tried to run the loop in new thread using [NSThread detachNewThreadSelector:...] which raises new problems (crashes every now and then).

Are iPhones released after 3G that much faster that delay/froze won't exist? I only have those 2 ancient devices to test with.

Upvotes: 1

Views: 1412

Answers (1)

Ryan Poolos
Ryan Poolos

Reputation: 18551

I would suggest either performing the creation in a background thread using this: (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg

Or I would try to rethink the process. Perhaps instead of creating a years worth of 2 week reminders at once just create a couple and set something else up to create the rest. Or maybe even just create one and store a repeating bool with it so when it fires it also creates the next one.

Upvotes: 1

Related Questions