iOS 原子操作autolically

备注以前认识的一个错误,关于iOS开发中原子的操作的问题;

API 内的方法说明

在NSData的writeToFile: autolically这个方法中看到API中这样一段解释:

1
2
3
flag 

If YES, the array is written to an auxiliary file, and then the auxiliary file is renamed to path. If NO, the array is written directly to path. The YES option guarantees that path, if it exists at all, won’t be corrupted even if the system should crash during writing.

大意为:

  1. flag设置为YES,在往文件写入东西时,我们会创建一个辅助的文件写入数据,写入完成之后,再修改为正确的路径;
  2. 当为NO时,直接写入文件,即便系统崩溃也不会中断,即有可能出现受污染的数据;

通常在读写文件的时候,为了防止意外造成数据污染,参数直接写为YES。

自己的见解

在修饰属性的时候,我们也经常会看到nonautomic这个修饰词,非原子的操作,这里没有使用原子操作的原因在于指针的操作都是瞬时的,原子非原子的操作都无所谓,我们约定一般都使用非原子的。

纠正错误

貌似我以前的认知中,这个属性是操作于是否可以并发访问一个属性的,但是想想,我们经常并发访问一个属性,倒也没什么影响,认识错误,纠正一下。