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

 星晴SOS 发布于 2022-10-28 17:35

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);
 });
    }
1 个回答
  • 不负责任的猜测:'startWithEndingBlock:' 是不是会开启计时器,不需要另外调用 'start' ?

    2022-10-29 20:18 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有