【问题标题】:How to execute a method at a certain date?如何在特定日期执行方法?
【发布时间】:2015-07-08 17:20:02
【问题描述】:

我的 Mac 应用程序会在特定时间更新它的徽章编号。
这是我的功能:

- (void)setBadgeNumber
{
    // Sets the badge number to 1.
    [[[NSApplication sharedApplication] dockTile] setBadgeLabel:@"1"];

    // Sends a notification.
    NSUserNotificationCenter *nc = [NSUserNotificationCenter defaultUserNotificationCenter];
    NSUserNotification *notification = [[NSUserNotification alloc] init];
    notification.title = @"Update";
    notification.informativeText = @"There's something new in the app!";
    [nc deliverNotification:notification];
}

我想知道您是否可以在某个日期(例如:08-08-2015)执行此功能。
即使应用程序未运行(即使应用程序正在运行)。
有人知道您是否可以在某个日期(和时间)执行方法吗?

【问题讨论】:

标签: objective-c macos cocoa badge


【解决方案1】:

你有很多选择,这取决于你追求什么。

明显的起点是 NSTimer。

还有带有延迟函数系列的 performSelector。

最后,我有时会使用一个很酷的扩展,延迟块。

哦,还有一个 - 您还可以设置本地通知。

我更喜欢 NSTimer 的initWithFireDate:interval:target:selector:userInfo:repeats:

更新

NSTimer:

NSTimer *saveProgressSizeTimer = [[NSTimer alloc]
    initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:2.0f]
    interval:1.0f
    target:self
    selector:@selector(myMethod:)
    userInfo:myUserInfo
    repeats:YES];  // This Will Be Executed After 2 Seconds

dispatch_after

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
    NSLog(@"parameter1: %d parameter2: %f", parameter1, parameter2);
});  // This Will Be Executed After 10 Seconds

【讨论】:

  • 你有例子吗?
【解决方案2】:

Launchd 通常与 Launch Agents 和 Launch Daemons 相关联,但 Apple 也建议将其作为 Unix cron 功能的替代品。

使用它,您可以create a launchd property listschedule jobs to run periodically 或在指定的日历间隔上。

如果您需要程序运行代码,请使用一个参数,当应用程序调用该参数时,该参数将调用相应的函数,然后退出。然后,通过配置属性列表并将其添加到正确的文件夹(例如 /Library/LaunchAgents),使用 launchd 在指定的日期和时间使用该参数调用您的程序

【讨论】:

  • 你有例子吗?
  • 什么例子?我已经链接到如何创建属性列表以及如何安排作业以日历间隔运行?如果你的意思是我有一个可行的例子给你,那么没有。但是,一旦您的应用程序处理为 passed arguments,那么您就拥有了所有需要的部分。
【解决方案3】:

声明一个 NSTimer 属性并调用您的方法来检查是否相同。

 self.nstimer=  [NSTimer scheduledTimerWithTimeInterval:0.10 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];

这将是你的更新方法:

- (int)updateTime:(NSTimer *)timer
 {

NSString *Time1 = @"2016-02-20 03:19:05 +0000";
NSString *Time2 = @"2016-02-20 03:19:05 +0000";
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss Z"];

NSDate *tokonExpireDate1=[dateFormatter dateFromString:Time1];
NSDate *tokonExpireDate2=[dateFormatter dateFromString:Time2];


if ([tokonExpireDate1 compare:tokonExpireDate2] == NSOrderedDescending) {
    //"date1 is later than date2
    NSLog(@"Later Date");

} else if ([tokonExpireDate1 compare:tokonExpireDate2] == NSOrderedAscending) {
    //date1 is earlier than date2
    NSLog(@"Previous Date");

} else {
    //dates are the same

    NSLog(@"Same Date"); 
    //CALL your method here.
}
}

我调用了该方法 0.10 秒。如果您需要按天间隔(2 天、3 天后等)调用它,请将您的日期转换为秒并调用。希望你知道这一点。它会起作用的。谢谢。

【讨论】:

    【解决方案4】:

    要在特定时间执行方法,请查看 NSTimer。但是,当应用程序当前未运行时,我不知道执行应用程序方法的任何方法。据我所知,只有退出方式可以在特定时间(使用给定参数)启动应用程序,或者使用 NSDockTilePlugin 等后台内容。

    【讨论】:

    • 但这不是计时器。它不计数到某个数字然后执行。它将在某个日期和时间执行。
    • 这正是 NSTimer 所做的。
    猜你喜欢
    • 1970-01-01
    • 2013-07-24
    • 2017-10-13
    • 2016-11-10
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 2010-09-22
    • 1970-01-01
    相关资源
    最近更新 更多