CoreData iOS - 如何创建对象的唯一ID? [英] CoreData iOS - How to create unique ID for objects?

查看:368
本文介绍了CoreData iOS - 如何创建对象的唯一ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iOS中有一个项目,用于iPhone和iPad等。对于项目,我使用CoreData来保存用户在应用程序中提供的数据。为了方便理解,让它像一个联系人的应用程序。所以基本上你知道你有你的名字,工作,电话等。这个应用程序还存储每个联系人的图像。当然可以。为了防止数据库效率低下,我使用以下路径代码将图像存储到DocumentsDirectory中。通过使用ManagedObjects id(也是图像名称中使用的)来调用图像。但是,多个对象可以有相同的id,这会导致一些问题。

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]]];

基本上我使用的人的id的图像。但是,多个人可以具有相同的id(An int-32)。而不是这样做,我想知道是否有一种方式CoreData创建或可以为每个存储的对象创建唯一的ID。这样,如果我更改id,它不会丢失它的图片或如果另一个对象具有相同的id,它不会使用相同的图片。

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.

一些数据库像MySQL我曾经使用有一个属性的选项,它会给它一个唯一的ID用于用户/论坛/帖子/ etc ... XCode的CoreData有类似的东西吗?我会讨厌通过每个对象来解析一个数字还没有被使用。

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.

推荐答案


一些数据库像一个MySQL我曾经使用...

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

使用Core Data时停止考虑MySQL。

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

托管对象具有唯一的ID字段,您可以使用 objectID 查找, NSManagedObjectID 类型。这是一个不透明的类,但你可以使用其 URIRepresentation 方法将其转换为 NSURL

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.

这些ID是唯一的,只要你只使用一个设备。如果您在设备之间同步数据,则一个设备上的ID将与其他设备上的ID不匹配。该ID用于Core Data的内部使用,并且只在同一持久存储文件中一致。

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.

一个更简单的方法是以使用UUID作为唯一ID。通过执行以下操作获取新的唯一ID:

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.

这篇关于CoreData iOS - 如何创建对象的唯一ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆