Link to a disk file from NSManagedObject

What would be the best way to name the file associated with NSManagedObject. The NSManagedObject will contain the URL of this file.

But I need to create a unique file name for my file. Is there some kind of auto-increment id that I could use? Should I use mktemp (but it is not a temporary file) or try to convert NSManagedObjectId to a file name? but I'm afraid that there will be special characters that may cause problems.

What would you suggest?

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

+4
source share
4 answers

There is a good way to do this, and one earlier answer almost had it - generate a GUID for each image, and not for the whole process. Call this method whenever you need a unique string, and then save it in a managed entity:

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

I use this to store captured movies and images.

+2
source

You can use NSProcessInfo to create a guid:

 [[NSProcessInfo processInfo] globallyUniqueString] 

And to refer to the file, I suggest just saving the directive as a property of NSManagedObject , and then just referring to the file by that name from the application support directory.

+3
source

I thought about using time to create a unique file name, but I do not like this solution, for example, if the time is reset or changed, there is a small chance to get the same file name twice.

I am surprised not to find more information about this topic on the Internet.

0
source

Since iOS5 now has the ability to store large drops in an external recording file. CoreData decides whether to store the record in sqlite depending on the size of the record.

Saving a drop in an external location using the built-in CoreData parameter

0
source

Source: https://habr.com/ru/post/1309234/


All Articles