CoreData iOS - How to create unique ID for objects?

可紊 提交于 2019-12-24 13:00:36

问题


I have a project in IOS for iPhones and iPads and such. For the project I am using CoreData to hold the data the user feeds in the app. For ease of understanding lets say its like a contacts app. So basically you know you have your name, work, phone etc. Well this app also stores an image for each "contact". Optionally of course. To prevent the database from being inefficient I store the image into the DocumentsDirectory using the below code for the path. The image is recalled by using the ManagedObjects id which is also what is used in name of image. However, multiple objects can have this same id, which causes some problems.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"Person%i.jpg",[[_detailItem id] intValue]]];

Basically I am taking the image using the id of the person. However, multiple people can have the same id(An int-32). Instead of doing this, I was wondering if there is a way that CoreData creates or can create a Unique id for each object stored. That way if I change the id it won't lose its picture or if another object has same id it won't use the same picture.

Some databases like a MySQL I used to use had an option for attributes that would give it a unique id for uses with users/forums/posts/etc... Is there something similar in XCode's CoreData? I would hate to have to parse through every object to get a number not being used yet.


回答1:


Some databases like a MySQL I used to use...

Stop thinking about MySQL when using Core Data. Doing so will only cause problems.

Managed objects have a unique ID field which you can look up using objectID, of type NSManagedObjectID. It's an opaque class, but you can convert it to an NSURL using its URIRepresentation method. From there you could convert it to a string and use that for an image filename.

These IDs are unique as long as you're only working with a single device. If you ever sync data between devices, the IDs on one device won't match those on the other device. The ID is intended for Core Data's internal use, and is only consistent within the same persistent store file.

If you really want an integer, you'll have to maintain it yourself. Store the value of the most recent integer in user defaults or in the persistent store file's metadata. When you create a new instance you'd read the integer, increment it, use it for the new instance, and save the new value.

A much simpler approach would be to use UUIDs for the unique IDs. Get a new unique ID by doing this:

NSString *newID = [[NSUUID UUID] UUIDString];

Store that as an attribute, and use it as part of the image filenames.




回答2:


Core Data does not have a way to do this automatically. You could parse the string form of the (permanent) objectID to get Core Data's unique identifier. A better option would be change your id to be a string and use a UUID.




回答3:


I'd suggest creating your own uniqueId attribute, and populating it with something like CFUUID, described in further detail here



来源:https://stackoverflow.com/questions/33504947/coredata-ios-how-to-create-unique-id-for-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!