We can set the primary key in the master data.

I am making an application in which I use basic data. Now in this I need to create a primary key in the same way as we create in sqlite (auto increment type). Is this possible with basic data. If so, then like any link or suggestion, to continue the same.

Relations Mrugen

+3
source share
4 answers

Since these cores are the foundation of strength, more than a general-purpose database, it abstracts the primary key.

+4
source

CoreData is not a database, but its solution for relational object mapping . This is a layer for saving objects. There is no concept of primary keys or foreign keys in CoreData.

. , CoreData , .

enter image description here

, "" , " ", .

.

+4

. , "id", "id", 1 .

"userID" ENTITY_USER (), .

+ (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;
}
+2

objectID NSManagedObject? .

+2
source

All Articles