UIApplication -scheduleLocalNotification:UIApplication -appWillTerminate调用时非常慢:

Car*_*loS 0 iphone performance cocoa-touch ios uilocalnotification

当我按下主页按钮时,我的应用程序的时间分析器

我在我的设置"应用程序不在后台运行" info.plist,所以当用户点击主页按钮时,应用程序退出.

当我 [UIApplication -appWillTerminate:]打电话时,我会向系统安排64个本地通知,所有这些都是不重复的.

但在带有iOS6.0.1的iPhone4上花费了相当长的时间(6.17秒).当我查看时间分析器时,我发现曲线非常奇怪,它不占用太多CPU时间,但确实需要花费很多时间.

此外,当我查看调用树时,93%的时间花费在[UIApplication -scheduleLocalNotification:]图像中显示的时间范围内.为什么?

这是我生成通知的方式:

UILocalNotification *n = [[[UILocalNotification] alloc] init] autorelease];
n.alertBody = @"some body";
n.hasAction = YES;
n.alertAction = @"some action";
n.fireDate = @"some date";
n.repeatInterval = 0;
n.soundName = @"my sound"
n.userInfo = aDictionaryWithAStringAbount10CharacterLongAnd2NSNumber.
[self.notifications addObject:n];
Run Code Online (Sandbox Code Playgroud)

这是我安排通知的方式:

-(void)endProxyAndWriteToSystemLocalNotification
{
        _proxying = NO;
        NSDate *dateAnchor = [NSDate date];

        NSEnumerator *enumerator = [self.notifications objectEnumerator];
        NSInteger i = 0;
        while (i < maxLocalNotifCount) {
            UILocalNotification *n = [enumerator nextObject];
            if (!d) {
                break;
            }

            if ([n.fireDate timeIntervalSinceDate:dateAnchor] >= 0) {
                [[UIApplication sharedApplication] scheduleLocalNotification:n];
                i++;
            }
        }

        [self.notificationDatas removeAllObjects];

}
Run Code Online (Sandbox Code Playgroud)

小智 5

这会有所帮助:

-(void)endProxyAndWriteToSystemLocalNotification {

    [[UIApplication sharedApplication] setScheduledLocalNotifications:self.notifications];
}
Run Code Online (Sandbox Code Playgroud)

iOS 4.2及更高版本

阅读UIApplication Class Reference以获取详细说明