Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Odd behavior of NSItemReplacementDirectory
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Odd behavior of NSItemReplacementDirectory

The behavior of NSItemReplacementDirectory is contrary to the docs and sort of suboptimal. Am I missing something or are the docs just wrong?

(1) The docs say the ‘appropriateForURL’ parameter is "a directory inside of which you want to create a unique temporary directory”. However, that isn’t the actual behavior on either platform:
	* On iOS 5, it’s created next to, not inside, the specified directory.
	* On Mac OS 10.7, it’s created in the default per-user temporary directory (/private/var/folders/...)
(2) Despite the fact that this is called a “temporary” directory, the documented location (and actual location on iOS) implies that it won’t be automatically deleted later on if my app crashes or otherwise forgets to clean up. That kind of defeats the purpose of using a temp dir!
(3) When called multiple times with the same ‘appropriateForURL’ path, it doesn’t return the same directory, but creates a new directory every time. This is cluttering up my filesystem with lots of empty directories named e.g. "(A Document Being Saved By MyAppName 21)”.

Here’s the code I’m using to create the temp dir:

        NSURL* parentURL = [NSURL fileURLWithPath: _path isDirectory: YES];
        NSURL* tempDirURL = [[NSFileManager defaultManager]
                                                 URLForDirectory: NSItemReplacementDirectory
                                                 inDomain: NSUserDomainMask
                                                 appropriateForURL: parentURL
                                                 create: YES error: &error];

Behavior (3) is leading me to suspect that I’m supposed to delete the directory when I’m done with it. Is that true? Also, if I get a new directory every time, I presumably can assume that it’s always empty and so I don’t have to worry about generating a unique filename for my temp file. Can I rely on that?

(My use case, FYI, is very simple: I’m downloading a file via a NSURLConnection. I don’t want to put the file in its final location until it’s completely downloaded, to avoid having an incomplete file left over if something goes wrong. So I want a temporary path to place it at that won’t be visible to the user, and will eventually be deleted if the app leaves it behind, but is guaranteed to be on the same filesystem as the final location so I can move it cheaply when it’s done. Basically all I want is a Cocoa equivalent of the old FindFolder call that returns the per-volume temp directory.)


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)

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

Visit the Apple Store online or at retail locations.

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.