核心数据(Mac)如何在ID字段上实现自动递增 [英] Core Data (Mac) How to implement auto increment on an ID field

查看:199
本文介绍了核心数据(Mac)如何在ID字段上实现自动递增的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人可以指向我如何在coredata实体中的属性上设置自动增量的方向。我知道索引的复选框只是使字段索引的搜索和查找速度。

I was wondering if anyone can point me in the direction of how to set up auto increment on an attribute in a coredata entity. I know the indexed check box just makes the field indexed for searching and lookup speeds.

我也知道,我必须在代码中这样做,但我不是确保如何获取最后一个/最高ID来设置新的对象ID。我发现如何做这年前,但不能再找到它。

I also know that I will have to do this in code, but I'm not sure on how to get the last/highest ID to set the new objects ID. I found how to do this years ago but can't find it again.

我对Objective-C的了解非常有限,对coredata更是如此。此外,如果有人可以建议如何确保字段是唯一的,例如名称字段,这将是巨大的。

I have a very limited knowledge of Objective-C and even more so of coredata. Also if someone could suggest how to make sure that a field is unique, such as the name field as well that would be great.

我的代码是这里,如果你想看看我到目前为止做了什么。该项目是一个基本的数据库,包含零件和位置以及工具。它只是部分完成,基本上是一个小数据库为我的人的电子套件。我确定有应用程序会做到这一点,但它总是更好当你把它做你自己

My code is here if you want to have a look at what I have done so far. The project is a basic database that contains parts and there locations and also tools. It's only partly finished, basically a small database for my person electronics kit. I'm sure there are apps that will do this but its always better when you make it your self

推荐答案

相当确定id在你的应用程序,但我的第一个观察是,你的数据模型中的所有实体都有一个名称和一个id,所以他们应该有一个抽象的超级实体来包含名称和id。除此之外,您可以在集合中检索最大ID,如下所示:

I'm not quite sure what the id does in your app, but my first observation is that all the entities in your data model have both a name and an id, so they should probably have an abstract super-entity to contain name and id. Other than that, you can retrieve the max id in a set like this:

Category *cat = /* get a category  */;
NSSet *parts = cat.parts;
NSNumber *maxID = [parts valueForKeyPath:@"@max.id"];

您可以对所有类别执行提取请求的类别的ID,然后使用此技术对返回的数组。我不知道你是否要保持每个模型对象的id是唯一的,或者你想让id从添加到每个实体的第一个对象开始。

for the id's of a category you could perform a fetch request for all the categories and then use this technique on the array that is returned. I don't know if you want to keep the id's of each model object unique or you want id's to start with 0 for the first object added to each entity.

确保字段是唯一的,您可以使用托管对象验证。这在核心数据编程指南中有更详细的描述,在模型对象实现指南中。确保名称是唯一的,需要您对validateName:error:方法中的所有名称进行测试。做一个抽象的超级实体会使这更容易。要做到这一点你得到抽象超级实体中的所有对象的名称,并测试它们对待验证的值。

To make sure a field is unique you can use managed object validation. this is described in the Core Data Programming Guide and in more detail in the Model Object Implementation Guide. Making sure that a name is unique would require you to test it against all the names in a validateName:error: method. making an abstract super entity will make this a lot easier. To do it you get all the names of objects in the abstract super entity and test them against the value to be validated.

编辑:如果你使用抽象超级实体,您应该获取抽象超级实体中的对象的最大id,如果您希望它们是唯一的。也有人指出,被管对象的objectID是一个唯一的标识符,它将永远是相同的,但看看应用程序我不认为这是真正你想要的,因为他们将是计算机数字,而不是当创建新对象时递增最高已知id的更友好的结果。另一件事你应该在awakeFromInsert中设置objet id,这样只有在创建对象时才会调用它。

If you use the abstract super entity then you should get the max id of the objects in the abstract super entity if you want them to be unique. Also as someone else pointed out the objectID of a managed object is a unique identifier for it that will always be the same, but looking at the app I don't think that's really what you want, because they will be computer numbers rather than the friendlier results of incrementing the highest known id when a new object is created. Another thing you should set the objet id in awakeFromInsert so that it is only called when the object is created.

这篇关于核心数据(Mac)如何在ID字段上实现自动递增的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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