iOS中使用dispatch_apply来重复执行任务,但计时器无法运行

新手上路,请多包涵

iOS中使用了MZTimerLabel做为计时器添加到任务中,然后使用dispatch_apply来重复执行任务,但计时器无法计时,而且不时在MZTimerLabel中实现的start方法中崩溃 [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
以下是我的代码,timer是引用MZTimerLabel中的第三方,可以在GitHub搜到。

    BLOCK myBlock = ^(size_t size){
        //如果是单词模式
        if (self.typer ==2) {
            [timer2 setCountDownTime:[self.timeCount integerValue]*60/2];
            [timer2 start];
            [timer2 startWithEndingBlock:^(NSTimeInterval countTime) {
                [self showView];
            }];
        }else{
            [timer2 setCountDownTime:[self.timeCount integerValue]*60];
            [timer2 start];
            [timer2 startWithEndingBlock:^(NSTimeInterval countTime) {
                agolab.text = @"休息时间";
                if([self.rest integerValue]!=0){
                    [timer2 start];
                    [timer2 setCountDownTime:[self.rest integerValue]*60];//休息时间
                    [timer2 startWithEndingBlock:^(NSTimeInterval countTime) {
                        agolab.text = @"已完成";
                    }];
                }
            }];
        }
        NSLog(@"执行次数");
    };
    
    //周期,多次执行的话
    if ([self.count integerValue] !=0) {
      dispatch_async(dispatch_get_global_queue(0, 0), ^{
            dispatch_apply([self.count integerValue], dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), myBlock);
 });
    }
阅读 3.4k
1 个回答
新手上路,请多包涵

不负责任的猜测:'startWithEndingBlock:' 是不是会开启计时器,不需要另外调用 'start' ?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题