Re: Creating a series of sequenced files
Re: Creating a series of sequenced files
- Subject: Re: Creating a series of sequenced files
- From: Carl Hoefs <email@hidden>
- Date: Sat, 16 Apr 2016 15:10:08 -0700
> Not that I know of. But surely it’s only 2 lines of code, apart from error checking and recovery?
I'd love to see those 2 lines of code! :-)
But I'm concerned mainly with efficiency, as determining the next number in the naming sequence potentially can be extremely inefficient - trying 1000s of times until the next available number every time a new file needs to be written, and also every time the latest-written file needs to be retrieved.
But I'm hearing this is the only way?
-Carl
> On Apr 16, 2016, at 2:58 PM, Quincey Morris <email@hidden> wrote:
>
> On Apr 16, 2016, at 14:36 , Carl Hoefs <email@hidden <mailto:email@hidden>> 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@hidden)
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:
This email sent to email@hidden