Master Data (Mac) How to implement auto-increment in ID field

I was wondering if anyone could tell me how to set up automatic attribute increment in a coredata object. I know that an indexed checkbox only makes the field indexed for search and search speed.

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

I have very limited knowledge of Objective-C and especially for coredata. Also, if someone can suggest how to make sure the field is unique, such as a name field, this will be great.

My code is here if you want to see what I have done so far. The project is a basic database that contains parts and locations, as well as tools. It only partially ended, basically a small database for my electronic electronic recruitment. I'm sure there are applications that will do this, but it is always better when you do it yourself.

+3
source share
2 answers

, id , , , , , , . , id , :

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

, . , , id 0 , .

, , . . , , validateName: error: method. - . - , .

: , id , , . - , objectID , , , , , , , , . , id awakeFromInsert, .

+1

apple:

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

Source: https://habr.com/ru/post/1786301/


All Articles