xcode 我们可以在核心数据中设置主键吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6386815/
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 21:17:22  来源:igfitidea点击:

can we set primary key in core data

iphonexcodecore-data

提问by mrugen

I am making a application in which i use core data . Now in this i need to create a primary key same as we create in sqlite (auto increment type ). Is this possible with core data .If yes then how any link or suggestion so as how to proceed for the same.

我正在制作一个使用核心数据的应用程序。现在,我需要创建一个与我们在 sqlite 中创建的主键相同的主键(自动增量类型)。这是否可能与核心数据。如果是,那么任何链接或建议如何进行相同的操作。

Regards Mrugen

问候姆鲁根

采纳答案by Peter DeWeese

Since core data is a persistance framework more than a general purpose database, it abstracts the primary key.

由于核心数据是一个持久性框架,而不是通用数据库,因此它抽象了主键。

回答by Shekhar Gupta

CoreData isn't a Database rather its an Object Relational Mapping Solution. It's an object persistence layer. There is no concept of primary keys or foreign keys in CoreData.

CoreData 不是数据库,而是一个对象关系映射解决方案。它是一个对象持久层。CoreData 中没有主键或外键的概念。

If you want to establish a relationship between two entities. You'll define a relationship, CoreData takes care of how that relationship is stored.

如果要在两个实体之间建立关系。您将定义一个关系,CoreData 负责该关系的存储方式。

enter image description here

在此处输入图片说明

Select an entity, use plus button at the bottom of the entities attributes list, select add relationship, select the destination entity from the dropdown.

选择一个实体,使用实体属性列表底部的加号按钮,选择添加关系,从下拉列表中选择目标实体。

Select the destination entity and define an inverse relationship in the same way.

选择目标实体并以相同方式定义反向关系。

回答by Paul Hunter

Couldn't you just use the objectID property of NSManagedObject? This would give you a unique reference to your object.

你不能只使用 NSManagedObject 的 objectID 属性吗?这将为您提供对您的对象的唯一引用。

回答by karim

The answer is NO. To achieve similar thing, You can have an 'id' field in the table, before inserting into the table get the maximum value of the 'id' and then add 1 to the value.

答案是不。为了实现类似的事情,您可以在表中有一个 'id' 字段,在插入表之前获取 'id' 的最大值,然后将该值加 1。

With a filed 'userID' in the table ENTITY_USER (users), the get the highest user ID.

使用表 ENTITY_USER (users) 中的归档“userID”,获得最高的用户 ID。

+ (NSInteger) getMaxUserID
{
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *res = [NSEntityDescription entityForName:ENTITY_USER
                                           inManagedObjectContext:[DataBaseManager sharedInstance].managedObjectContext];
    [request setEntity:res];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"userID" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [request setSortDescriptors:sortDescriptors];
    [sortDescriptors release];
    [sortDescriptor release];

    [request setFetchLimit:1];

    NSError *error = nil;
    NSArray *results = [[DataBaseManager sharedInstance].managedObjectContext executeFetchRequest:request error:&error];
    [request release];
    if (results == nil) {
        NSLog(@"error fetching the results: %@",error);
    }

    NSInteger maximumValue = 0;
    if (results.count == 1) {
        CDUser *result = (CDUser *)[results objectAtIndex:0];
        maximumValue =  [result.userID integerValue];
    }
    return maximumValue;
}