Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

writeToFile:atomically: what does atomically mean?

I am wondering what the atomically: parameter stands for in the writeToFile:atomically: method (-[NSArray writeToFile:atomically:] for example). It is common to pass YES for atomically:, but I don't know what it means.

like image 258
JonasG Avatar asked Dec 17 '11 22:12

JonasG


People also ask

What are atomic files?

Atomic file guarantees file integrity by ensuring that a file has been completely written and sync'd to disk before renaming it to the original file.

How do you write to a file in Objective C?

Having stored the contents of a file in an NSData object, that data may subsequently be written out to a new file using the createFileAtPath method: databuffer = [filemgr contentsAtPath: @"/tmp/myfile. txt" ]; [filemgr createFileAtPath: @"/tmp/newfile. txt" contents: databuffer attributes: nil];


2 Answers

An 'atomic write' is one where you are guaranteed to have either a correct, complete write to the file or an error. There's no chance that, say, half of the write will work and then something bad happens (lost power, drive crash, etc) and the rest of the write fails. It's all or nothing. This is generally what you want.

like image 167
Jack Danger Avatar answered Sep 20 '22 08:09

Jack Danger


atomically

If YES, the data is written to a backup file, and then—assuming no errors occur—the backup file is renamed to the name specified by path; otherwise, the data is written directly to path.

like image 21
Darko Kenda Avatar answered Sep 18 '22 08:09

Darko Kenda