views:

70

answers:

3

Hello,

What would be the best way to name a file associated to a NSManagedObject. The NSManagedObject will hold the URL to this file.

But I need to create a unique filename for my file. Is there some kind of autoincrement id that I could use? Should I use mktemp (but it's not a temporary file) or try to convert the NSManagedObjectId to a filename? but I fear there will be special characters which might cause problem.

What would you suggest?

EDIT: I have a lot of these NSManagedObjects and each has its own image, so I want to generate a unique name for each picture.

+1  A: 

You can use NSProcessInfo to generated the guid:

[[NSProcessInfo processInfo] globallyUniqueString]

And to reference a file I'd suggest just keeping the guid as NSManagedObject property and then just reference a file by that name from application support directory.

Eimantas
Thanks. However, I have a lot of these NSManagedObjects. They all have images. My question was more how to differentiate the images from the different NSManagedObjects.
Kamchatka
A: 

I thought about using the time to generate a unique filename but I don't like the solution, for instance if the time is reset or changed, there is a slight chance of getting two times the same filename.

I'm surprised not to find more information about this subject on the web.

Kamchatka
+1  A: 

There is a good way to do this and one earlier answer almost had it – generate a GUID for each image instead of for the entire process. Call this method whenever you need a unique string, and then store it in the managed object:

+ (NSString *)getUUID
{
    CFUUIDRef theUUID = CFUUIDCreate(NULL);
    CFStringRef string = CFUUIDCreateString(NULL, theUUID);
    CFRelease(theUUID);
    return [(NSString *)string autorelease];
}

I use this for storing captured movies and images.

Peter DeWeese