Referencing file on disk from NSManagedObject
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 auto开发者_如何学Goincrement 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.
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.
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.
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.
Since iOS5 there is now the option to store large blobs in an external record file. CoreData decides whether or not to store the record in sqlite based on the size of the record.
Storing blobs in external location using built-in CoreData option
精彩评论