Iphone 在新线程上设置本地通知?

Iphone 在新线程上设置本地通知?,iphone,multithreading,ios4,nsthread,uilocalnotification,Iphone,Multithreading,Ios4,Nsthread,Uilocalnotification,我需要知道是否有可能创建一个新线程来处理设置本地通知 我的应用程序在很大程度上依赖于这些通知,因此我希望在手机设置通知时让应用程序正常工作 例如: (现在) 您启动应用程序,应用程序挂起在启动屏幕上设置本地通知,然后启动 (我想要) 当设置本地通知时,应用程序启动并可用 我也需要一些示例代码,请:) (作为记录,由于我自己的原因,每次应用程序进入前台时,我都会设置60个本地通知…) 谢谢 执行线程的一种方法是使用performSelectorInBackground 例如: [myObj per

我需要知道是否有可能创建一个新线程来处理设置本地通知

我的应用程序在很大程度上依赖于这些通知,因此我希望在手机设置通知时让应用程序正常工作

例如:

(现在)

您启动应用程序,应用程序挂起在启动屏幕上设置本地通知,然后启动

(我想要)

当设置本地通知时,应用程序启动并可用

我也需要一些示例代码,请:)

(作为记录,由于我自己的原因,每次应用程序进入前台时,我都会设置60个本地通知…)


谢谢

执行线程的一种方法是使用
performSelectorInBackground

例如:

[myObj performSelectorInBackground:@selector(doSomething) withObject:nil];

但是,您应该注意到,苹果非常强烈地建议您使用更高级别的概念,如
NSOperation
s和调度队列,而不是显式生成线程。请参见

是的,这是可以做到的,我一直都这样做:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Add the navigation controller's view to the window and display.
    [NSThread detachNewThreadSelector:@selector(scheduleLocalNotifications) toTarget:self withObject:nil];
    [window addSubview:navigationController.view];
    [window makeKeyAndVisible];

    return YES;
}

-(void) scheduleLocalNotifications
{

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

    for (int i = 0; i < 60; i++)
    {
        UILocalNotification *localNotif = [[UILocalNotification alloc] init];
        if (localNotif == nil)
            return;
        NSDate *sleepDate = [[NSDate date] dateByAddingTimeInterval:i * 60];
        NSLog(@"Sleepdate is: %@", sleepDate);

        localNotif.fireDate = sleepDate;    

        NSLog(@"fireDate is %@",localNotif.fireDate);
        localNotif.timeZone = [NSTimeZone defaultTimeZone];
        localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(@"This is local notification %i"), i];

        localNotif.alertAction = NSLocalizedString(@"View Details", nil);
        localNotif.soundName = UILocalNotificationDefaultSoundName;
        localNotif.applicationIconBadgeNumber = 1;

        [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
        NSLog(@"scheduledLocalNotifications are %@", [[UIApplication sharedApplication] scheduledLocalNotifications]);
        [localNotif release];

    }

    [pool release];
}
-(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项{
//将导航控制器的视图添加到窗口并显示。
[NSThread detachNewThreadSelector:@selector(scheduleLocalNotifications)to target:self with object:nil];
[窗口添加子视图:navigationController.view];
[WindowMakeKeyandVisible];
返回YES;
}
-(无效)scheduleLocalNotifications
{
NSAutoreleasePool*池=[[NSAutoreleasePool alloc]init];
对于(int i=0;i<60;i++)
{
UILocalNotification*localNotif=[[UILocalNotification alloc]init];
if(localNotif==nil)
返回;
NSDate*sleepDate=[[NSDate date]date BY ADDINGTIMETERVAL:i*60];
NSLog(@“Sleepdate为%@”,Sleepdate);
localNotif.fireDate=sleepDate;
NSLog(@“fireDate是%@”,localNotif.fireDate);
localNotif.timeZone=[NSTimeZone defaultTimeZone];
localNotif.alertBody=[NSString stringWithFormat:NSLocalizedString(@“这是本地通知%i”),i];
localNotif.alertAction=NSLocalizedString(@“查看详细信息”,无);
localNotif.soundName=UILocalNotificationDefaultSoundName;
localNotif.applicationBadgeNumber=1;
[[UIApplication sharedApplication]scheduleLocalNotification:localNotif];
NSLog(@“ScheduledLocalNotification为%@”,[[UIApplication sharedApplication]ScheduledLocalNotification]);
[localNotif发布];
}
[池释放];
}
从一个项目,我现在正在工作,我可以确认它的工作预期

编辑:

示例在
scheduleLocalNotifications
中泄漏,因为缺少处理
NSAutoreleasePool
,现在已添加到示例中。

感谢您的贡献!它的撞击力更大(嘿,np..这就是我们在这里讨论的问题。在主线程外使用UIApplication是不安全的,可能会导致“意外结果”。我想问一下为什么需要在线程外执行。我怀疑这会占用很多时间。如果需要在线程外执行,您仍然需要在主线程上运行“scheduleLocalNotification”以确保安全。