xcode Core Data (Mac) 如何在 ID 字段上实现自动递增

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4731503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:06:59  来源:igfitidea点击:

Core Data (Mac) How to implement auto increment on an ID field

objective-cxcodecore-dataauto-increment

提问by Ashley Hughes

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.

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

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.

我也知道我必须在代码中执行此操作,但我不确定如何获取最后一个/最高 ID 以设置新对象 ID。几年前我找到了如何做到这一点,但再也找不到了。

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.

我对Objective-C 的了解非常有限,对coredata 的了解更甚。此外,如果有人可以建议如何确保一个字段是唯一的,例如名称字段,那会很棒。

My code is hereif 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

如果你想看看我到目前为止所做的事情,我的代码就在这里。该项目是一个包含零件和位置以及工具的基本数据库。它只是部分完成,基本上是我个人电子套件的小型数据库。我相信有一些应用程序可以做到这一点,但当你自己动手时它总是更好

采纳答案by Rich

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:

我不太确定 id 在您的应用程序中的作用,但我的第一个观察是您的数据模型中的所有实体都有一个名称和一个 id,因此它们可能应该有一个抽象的超级实体来包含名称和 id . 除此之外,您可以像这样检索集合中的最大 id:

Category *cat = /* get a category  */;
NSSet *parts = cat.parts;
NSNumber *maxID = [parts valueForKeyPath:@"@max.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.

对于类别的 id,您可以对所有类别执行 fetch 请求,然后在返回的数组上使用此技术。我不知道您是要保持每个模型对象的 id 唯一,还是希望添加到每个实体的第一个对象的 id 以 0 开头。

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.

要确保字段是唯一的,您可以使用托管对象验证。这在 Core Data Programming Guide 中有描述,在 Model Object Implementation Guide 中有更详细的描述。确保名称是唯一的需要您针对 validateName:error: 方法中的所有名称对其进行测试。制作一个抽象的超级实体将使这变得容易得多。为此,您需要获取抽象超级实体中对象的所有名称,并根据要验证的值对它们进行测试。

Edit: 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.

编辑:如果您使用抽象超级实体,那么如果您希望它们是唯一的,那么您应该获得抽象超级实体中对象的最大 id。另外,正如其他人指出的,托管对象的 objectID 是它的唯一标识符,它始终相同,但是查看应用程序我认为这并不是您真正想要的,因为它们将是计算机编号而不是在创建新对象时增加已知最高 id 的结果更友好。另一件事您应该在awakeFromInsert 中设置对象ID,以便仅在创建对象时调用它。

回答by Rich

this is how apple implemented autoincrement:

这就是苹果实现自动增量的方式:

 - (void) awakeFromInsert
     {
    [super awakeFromInsert];
    static int tempID = 0;
    [self setValue:[NSNumber numberWithInt:++tempID] forKey:@"id"];
}