The identifier of the object with automatic addition to the base data?

I work with several types of NSManagedObject with multiple relationships. How can I tell Core Data to automatically populate object identifiers for me? I am looking for something like an index key in SQL, so no instance of this object is allowed to have the same identifier.

Edit:

I would like all of my Account objects to have unique identifiers. I just added it to `countForFetchRequest, but I realized that when deleting the second and last objects, and then adding one of them, the last two objects have the same identifiers.

How can I guarantee that the given value has a unique value for all instances of my NSManagedObject "account"?

EDIT2:

I need to have a separate id for sorting.

+6
source share
4 answers

How I solved it with Core Data aggregates. Actually, I assign an identifier myself.

Essentially, I query Core Data for all the entity identifiers of my entity, and then iterate over them. If I find an identifier that exceeds the current temporary, I make the temporary identifier higher than one higher than the aggregated one. When I'm done, I will automatically have an ID that is higher than the highest in the list. The only drawback that I see in this is the lack of an identifier. (I believe there is a simple fix for this).

 // // Create a new entity description // NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:self.managedObjectContext]; // // Set the fetch request // NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease]; [fetchRequest setEntity:entity]; // // We need to figure out how many // existing groups there are so that // we can set the proper ID. // // To do so, we use an aggregated request. // [fetchRequest setResultType:NSDictionaryResultType]; [fetchRequest setPropertiesToFetch:[NSArray arrayWithObject:@"entityID"]]; NSError *error = nil; NSArray *existingIDs = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error]; if (error != nil) { // // TODO: Handle error. // NSLog(@"Error: %@", [error localizedDescription]); } NSInteger newID = 0; for (NSDictionary *dict in existingIDs) { NSInteger IDToCompare = [[dict valueForKey:@"entityID"] integerValue]; if (IDToCompare >= newID) { newID = IDToCompare + 1; } } // // Create the actual entity // MyEntity *newEntity = [[MyEntity alloc] initWithEntity:entity insertIntoManagedObjectContext:self.managedObjectContext]; // // Set the ID of the new entity // [newEntity setEntityID:[NSNumber numberWithInteger:newID]]; // // ... More Code ... // 
+4
source

All NSManagedObjects automatically have a unique NSManagedObjectID . There is no concept of a custom auto-increment attribute, but it is certainly easy to write yourself.

+10
source

Getting started with your EDIT2 and Edit3, the following answer will help you. Assume your id field as NSNumber having unsignedInt as an identifier.

1) Retrieve all entries for the corresponding object.

 NSError *error = nil; NSArray *array = [self fetchAllFileEntity:&error]; 

2) Find the maximum number belonging to this result.

 NSNumber *maxValue = nil; if (array) maxValue = [array valueForKeyPath:@"@max.uniqueId.unsignedIntegerValue"]; else maxValue = [NSNumber numberWithUnsignedInteger:0]; 

3) Assign maxValue + 1 to your new object

 entity.uniqueId = [NSNumber numberWithUnsignedInteger:maxValue.unsignedIntegerValue+1]; 
+3
source

I came up with this solution for the indicated problem, I hope that it will be useful for someone.

 AppDelegate *appdelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate]; NSManagedObjectContext *context = [appdelegate managedObjectContext]; NSError *error = nil; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *chatHist = [NSEntityDescription entityForName:@"ChatHistory" inManagedObjectContext:context]; [fetchRequest setEntity:chatHist]; int chatIdNumber = 0; NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error]; if ([fetchedObjects count] > 0) { ChatHistory *chatHistObj = [fetchedObjects objectAtIndex:[fetchedObjects count]-1]; chatIdNumber = [chatHistObj.chatId intValue]; } chatIdNumber = chatIdNumber+1; ChatHistory *chat_History = [NSEntityDescription insertNewObjectForEntityForName:@"ChatHistory" inManagedObjectContext:context]; chat_History.chatId = [NSString stringWithFormat:@"%d",chatIdNumber]; 
+1
source

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


All Articles