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

发布于 2024-10-12 09:54:53 字数 433 浏览 3 评论 0原文

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

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

我对 Objective-C 的了解非常有限,对 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.

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凯凯我们等你回来 2024-10-19 09:54:54

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

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

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

为了确保字段是唯一的,您可以使用托管对象验证。 《核心数据编程指南》对此进行了描述,《模型对象实现指南》对此进行了更详细的描述。要确保名称是唯一的,您需要针对 validateName:error: 方法中的所有名称对其进行测试。制作一个抽象的超级实体将使这变得容易得多。为此,您需要获取抽象超级实体中的所有对象名称,并根据要验证的值对其进行测试。

编辑:如果您使用抽象超级实体,那么您应该获取抽象超级实体中对象的最大 id(如果您希望它们是唯一的)。另外,正如其他人指出的那样,托管对象的 objectID 是它的唯一标识符,它将始终相同,但查看应用程序,我认为这并不是您真正想要的,因为它们将是计算机编号而不是创建新对象时增加已知的最高 id 会产生更友好的结果。另外,您应该在 awakeFromInsert 中设置对象 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"];

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.

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.

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.

挽袖吟 2024-10-19 09:54:54

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

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

this is how apple implemented autoincrement:

 - (void) awakeFromInsert
     {
    [super awakeFromInsert];
    static int tempID = 0;
    [self setValue:[NSNumber numberWithInt:++tempID] forKey:@"id"];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文