scheduleLocalNotification的性能问题(大量本地通知)

时间:2012-10-09 07:50:05

标签: iphone ios performance uilocalnotification localnotification

我正面临scheduleLocalNotification的性能问题。 我正在尝试注册大量的本地通知。 这就像朋友的生日闹钟。为了测试,我尝试注册大约300个通知,但我的iPhone4花了2分钟。 (iPad2 4秒,iPhone4S 8秒)

这是一段代码。

-(void)setAllBirthdaysSchedule
{
    Class cls = NSClassFromString(@"UILocalNotification");
    if (cls == nil) {
        return ;
    }

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    if (prototypeNotification == nil) {
        prototypeNotification = [[UILocalNotification alloc] init];
        prototypeNotification.repeatCalendar = [NSCalendar currentCalendar];
        prototypeNotification.repeatInterval = NSYearCalendarUnit;

        prototypeNotification.timeZone = [NSTimeZone defaultTimeZone];
        prototypeNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0];
        prototypeNotification.applicationIconBadgeNumber = 0;
        prototypeNotification.alertBody = NSLocalizedString(@"Body", nil);
        prototypeNotification.alertAction = NSLocalizedString(@"Action", nil);
    }

    NSArray* arr = [self getAllBirthday];

    for (User* user in arr) {                
        UILocalNotification *notif = [prototypeNotification copy];
        notif.fireDate = user.birthday;
        [[UIApplication sharedApplication] scheduleLocalNotification:notif];
        [notif release];

    }

    [pool release];
}

1 个答案:

答案 0 :(得分:1)

是的,在较旧的设备上注册本地通知需要一些时间。尝试将注册放入后台线程。

请注意,iOS中最多有64个通知,其余的将被丢弃。有关更多信息,请参阅UILocalNotification类参考。