On Apr 16, 2016, at 14:36 , Carl Hoefs <[email protected]> wrote:
> 
> I have a daemon process that needs to generate a series of sequenced files 
> (named sequentially, such as "file_01944576_1.dat", "file_01944576_2.dat", 
> etc.) in the same directory. Does Cocoa provide a way to do this?

Not that I know of. But surely it’s only 2 lines of code, apart from error 
checking and recovery?

> NSFileManager's -createFileAtPath:contents:attributes: method states:
> "If a file already exists at path, this method overwrites the contents of 
> that file…"

Do not use this method: it’s obsolete. Use NSData.writeToURL:options: instead, 
with the NSDataWritingWithoutOverwriting option.

> I would hate to do this blindly, such as with fstat() in a loop, because 
> there will potentially be many sequences, and each can grow to an arbitrary 
> number. How do Finder and other OS X agents accomplish this?

Blindly in what sense? Even you have a lot of files, those are the files you 
want. There’s nothing inherently dangerous in creating them. If you mean, 
because of the possibility of conflicts with existing files, you should follow 
the normal best practices inside your ‘writeToFile’ loop:

— (Optional) Preflight the loop to ensure that conflicting files don’t exist. 
This is only an approximate safety measure, since conflicting files can be 
created after you check, but provides a better user experience if there is some 
reasonable expectation of conflict.

— Detect any actual conflict by the failure of ‘writeToFile’ and offer user 
recovery options at that point.

_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to