Probably NSURLSessionDownloadTask fits this requirement. It downloads the data 
to a temporary file which can be read or moved to a permanent location. The 
temporary file gets deleted as soon as control returns from the completion 
handler. 

------Original message------
From: Jens Alfke <j...@mooseyard.com>
To: "Cocoa-dev@lists.apple.com dev" <cocoa-dev@lists.apple.com>
Date: Tuesday, September 2, 2014 9:47:37 AM GMT-0700
Subject: Best practice for creating a temporary file

I'm downloading data that will eventually be saved as a file in a specific 
directory. During the download I write the data to a temporary file since it 
can be arbitrarily large; then on completion I move the temporary file to its 
final location. So I need to locate a temporary-files directory that is on the 
same volume as the final location.

So far I've been using NSItemReplacementDirectory, but this has a couple of 
problems:
(1) It creates a new visible directory right next to the permanent location, 
called "(A Document being saved by $APP") where $APP is the app name.
(2) This directory doesn't get cleaned up by the OS. I have to remember to 
delete it, and if the app unexpectedly quits it gets left behind and then the 
next launch creates another of these directories.

What's the alternative? I don't want to use NSTemporaryItemsDirectory because 
that's always on the boot volume, and the final location of the file might not 
be; in that case the move operation will turn into a copy-and-delete which will 
be non-atomic and could be slow.

It looks as though there _is_ a per-volume temporary-items directory, because 
on my non-boot partition I see a top-level invisible directory called 
".TemporaryItems" with a subfolder for each user-ID. But how do I find it 
programmatically?

—Jens
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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/excelpbhardwaj999%40gmail.com

This email sent to excelpbhardwaj...@gmail.com

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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 arch...@mail-archive.com

Reply via email to