アプリがバックグラウンドに移行したときに大量の通知をスケジュールしようとしています。次のコードはiPhoneシミュレーターでうまく機能します...
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"applicationDidEnterBackground");
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
// Clean up any unfinished task business by marking where you.
// stopped or ending the task outright.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task, preferably in chunks.
// Get notifications count
int notificationsCount = [[[UIApplication sharedApplication] scheduledLocalNotifications] count];
// Calculate the number of possible remaining notifications (maximum 64 per application)
int notificationsRemaining = 64 - notificationsCount;
if (notificationsCount > 0) {
// Get the last notification currently scheduled
UILocalNotification *lastNotif = [[[UIApplication sharedApplication] scheduledLocalNotifications] lastObject];
// If the last notification is not repeating notification
if (lastNotif.repeatInterval == 0) {
// Get the last notification's values
NSDictionary *userInfo = lastNotif.userInfo;
NSInteger interval = [[userInfo objectForKey:@"interval"] intValue];
NSDate *fireDate = lastNotif.fireDate;
NSTimeZone *timeZone = lastNotif.timeZone;
NSString *alertBody = lastNotif.alertBody;
bool hasAction = lastNotif.hasAction;
NSString *alertAction = lastNotif.alertAction;
NSString *soundName = lastNotif.soundName;
// Schedule the remaining notifications
for (int i = 1 ; i < notificationsRemaining+1 ; i++) {
// Allocate new local notification to be scheduled
UILocalNotification *notif = [[UILocalNotification alloc] init];
// If notif is nil go back one step and try again
if (notif == nil) {
i--;
} else {
// Set up the newly created notification
notif.fireDate = [fireDate dateByAddingTimeInterval: 10 * interval * i];
notif.timeZone = timeZone;
notif.alertBody = alertBody;
notif.hasAction = hasAction;
notif.alertAction = alertAction;
notif.soundName = soundName;
notif.userInfo = userInfo;
notif.applicationIconBadgeNumber += 1;
// Schedule the newly created notification
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
}
}
}
}
NSLog(@"notifications: %@", [[UIApplication sharedApplication] scheduledLocalNotifications]);
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}
...しかし、iPod Touch 2ではエラーが発生して失敗します:
Thu May 24 14:03:18 unknown TestApp[599] <Warning>: applicationDidEnterBackground
Thu May 24 14:03:18 unknown TestApp[599] <Warning>: applicationWillTerminate
Thu May 24 14:03:18 unknown com.apple.launchd[1] <Notice>: (UIKitApplication:my.test.TestApplicationWithPhoneGap[0x1c13]) Bug: launchd_core_logic.c:3252 (24226):3
Thu May 24 14:03:18 unknown com.apple.launchd[1] <Notice>: (UIKitApplication:my.test.TestApplicationWithPhoneGap[0x1c13]) Bug: launchd_core_logic.c:2681 (24226):10
Thu May 24 14:03:18 unknown com.apple.launchd[1] <Notice>: (UIKitApplication:my.test.TestApplicationWithPhoneGap[0x1c13]) Working around 5020256. Assuming the job crashed.
Thu May 24 14:03:18 unknown com.apple.launchd[1] <Warning>: (UIKitApplication:my.testTestApplicationWithPhoneGap[0x1c13]) Job appears to have crashed: Segmentation fault
Thu May 24 14:03:18 unknown com.apple.debugserver-48[597] <Warning>: 1 [0255/1403]: error: ::read ( 4, 0x2fee59f0, 1024 ) => -1 err = Bad file descriptor (0x00000009)
Thu May 24 14:03:18 unknown SpringBoard[24] <Warning>: Application 'TestApp' exited abnormally with signal 11: Segmentation fault
Thu May 24 14:03:24 unknown SpringBoard[24] <Warning>: Unable to cancel system wake for 2012-05-24 11:03:09 +0000. IOPMCancelScheduledPowerEvent() returned 0xe00002f0
これが実際のデバイスで機能しない理由、これを修正する方法はありますか?
編集された回答:
旧世代のiPodTouchとiPhoneはこれをサポートしていないようです