Simultaneous accesses to 0x1c5a674c8, but modification requires exclusive access.

错误环境:

计时器计算,time是一个自定义结构体。

 func countTimeWithConstantTime(time:timeStruct){
        self.currentTime = time
        timer.scheduleRepeating(wallDeadline: DispatchWallTime.now(), interval: 1.0)
        timer.setEventHandler {
//            let second = self.currentTime?.second
            self.currentTime?.second = (self.currentTime?.second)! - 1
            NVRLOG(self.currentTime?.second)
        }
        timer.resume()
    }

错误分析:

 报错如标题,错误主要是在这一句 

self.currentTime?.second = (self.currentTime?.second)! - 1

这是一个常规操作,如果计算的是基本数据类型,必然不会报错。

现在currentTime是一个结构体,存在内存里。进行计算的时候都会进行IO(读写)操作,这样的话线程就不安全了,就是最开始学习的资源抢夺问题。

错误解决:

  

  let second = self.currentTime?.second
  self.currentTime?.second = second! - 1

这样每次都是一个赋值,然后再从新赋值回去




猜你喜欢

转载自blog.csdn.net/zy_flyway/article/details/80494187