使用scheduleLocalNotification(大量本地通知)存在性能问题

4

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

能否请管理员重新开放这个问题?“重复”实际上是针对本地通知限制的,而这个问题则涉及性能问题。 - Mazyod
1个回答

1

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

请注意,在iOS中最多只能有64个通知,其余的将被丢弃。查看UILocalNotification类参考获取更多信息。


谢谢。我尝试在后台线程中运行它,但是即使回到主屏幕,设备几乎也会冻结。这很奇怪。 - samtaegi
@samtaegi,我也亲眼见证了这一点。我通过每个runloop处理一个通知来解决了这个问题。只需创建一个操作队列,并在主线程上异步地逐个处理操作。假设主线程每秒运行60次,它应该能够平均分配负载,同时仍然给主线程时间来工作。 - Mazyod

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接