问题
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
回答1:
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.
回答2:
this is how apple implemented autoincrement:
- (void) awakeFromInsert
{
[super awakeFromInsert];
static int tempID = 0;
[self setValue:[NSNumber numberWithInt:++tempID] forKey:@"id"];
}
来源:https://stackoverflow.com/questions/4731503/core-data-mac-how-to-implement-auto-increment-on-an-id-field