Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 定时触发器_Objective C_Cocoa_Nstimer - Fatal编程技术网

Objective c 定时触发器

Objective c 定时触发器,objective-c,cocoa,nstimer,Objective C,Cocoa,Nstimer,我试图创建一个控制方法,它在不同的线程中执行子方法。我已经研究了NSTimer,但有几个子方法并没有在精确的时间间隔内执行 以下是5个时间间隔: 每0.1秒快速执行一次 每5分钟执行一次 每10分钟执行一次 每小时执行一次 每天执行(午夜) 间隔1-3应该很容易用NSTimer完成。实施其他方法有什么建议吗 提前谢谢 对于(4)和(5),如果NSTimer不够准确,您需要dispatch\u walltime /* timespec for midnight, new years day 201

我试图创建一个控制方法,它在不同的线程中执行子方法。我已经研究了NSTimer,但有几个子方法并没有在精确的时间间隔内执行

以下是5个时间间隔:

  • 每0.1秒快速执行一次
  • 每5分钟执行一次
  • 每10分钟执行一次
  • 每小时执行一次
  • 每天执行(午夜)
  • 间隔1-3应该很容易用NSTimer完成。实施其他方法有什么建议吗

    提前谢谢

    对于(4)和(5),如果
    NSTimer
    不够准确,您需要
    dispatch\u walltime

    /* timespec for midnight, new years day 2014 */
    
    struct tm time = {
       .tm_sec = 0,
       .tm_min = 0,
       .tm_hour = 0,
       .tm_mday = 1,
       .tm_mon = 1,
       .tm_year = 2014,
       .tm_wday = -1,  // I think -1 is wildcard here, can someone help?
       .tm_yday = -1,
       .tm_isdst = TRUE
    };
    
    const struct timespec whenToFire = {
       .tv_sec = mktime(time),
       .tv_nsec = 0
    };
    
    dispatch_time_t time = dispatch_walltime(&whenToFire,0);
    
    /* a lower-priority queue may delay firing */    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT);
    
    dispatch_source_t timeSrc = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 
                                      0, 0, queue);
    
    dispatch_source_set_timer(timeSrc, time, 100000 , 100);
    
    dispatch_source_set_event_handler(timeSrc, ^{   
        /* only run once */
        static dispatch_once_t token;
        dispatch_once(&token, ^{
            /* code to run on New Years Day here */
            dispatch_source_cancel(timeSrc);
        });
    });
    
    对于(4)和(5),如果
    NSTimer
    不够准确,则需要
    dispatch\u walltime

    /* timespec for midnight, new years day 2014 */
    
    struct tm time = {
       .tm_sec = 0,
       .tm_min = 0,
       .tm_hour = 0,
       .tm_mday = 1,
       .tm_mon = 1,
       .tm_year = 2014,
       .tm_wday = -1,  // I think -1 is wildcard here, can someone help?
       .tm_yday = -1,
       .tm_isdst = TRUE
    };
    
    const struct timespec whenToFire = {
       .tv_sec = mktime(time),
       .tv_nsec = 0
    };
    
    dispatch_time_t time = dispatch_walltime(&whenToFire,0);
    
    /* a lower-priority queue may delay firing */    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT);
    
    dispatch_source_t timeSrc = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 
                                      0, 0, queue);
    
    dispatch_source_set_timer(timeSrc, time, 100000 , 100);
    
    dispatch_source_set_event_handler(timeSrc, ^{   
        /* only run once */
        static dispatch_once_t token;
        dispatch_once(&token, ^{
            /* code to run on New Years Day here */
            dispatch_source_cancel(timeSrc);
        });
    });
    

    这是在OSX中使用基础CLI实现的前3个,但是在运行它时,应用程序的CPU使用率相当高。这就是NSTimer的工作原理吗?简单的while循环会更有效吗?这是在OSX中使用的基础CLI实现了前3个,但是在运行它时,应用程序的CPU使用率相当高。这就是NSTimer的工作原理吗?简单的while循环会更有效吗?不是NSTimer不够精确,而是选择器没有按周期间隔执行。比如说,节目在7:30开始。我希望方法4在8、9等时间执行…我不能只使用timerWithTimeInterval方法。是的,但您可以使用
    -initWithFireDate:interval:target:selector:userInfo:repeats:
    这两种方法都要求您单独安排每次运行,但这相对容易,您只需在当前运行时安排新运行。实际上,
    initWithFireDate
    不要求您单独安排每次运行。只需将一个设置为下午4点点火,将间隔设置为3600秒。这不是因为NSTimer不够精确,而是选择器没有按周期间隔执行。比如说,节目在7:30开始。我希望方法4在8、9等时间执行…我不能只使用timerWithTimeInterval方法。是的,但您可以使用
    -initWithFireDate:interval:target:selector:userInfo:repeats:
    这两种方法都要求您单独安排每次运行,但这相对容易,您只需在当前运行时安排新运行。实际上,
    initWithFireDate
    不要求您单独安排每次运行。只需设置一个在下午4点点火,设置间隔为3600秒。