我遇到了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];
}