Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: Core Data and auto increment
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Core Data and auto increment




On Jul 29, 2006, at 6:38 AM, Eric Morand wrote:

I have a customer entity that contains a "customer number" attribute. I want this attribute to be automatically generated when a customer is inserted into the database (SQLite). In other words, I want the customer number attribute to auto-increment.

I just had the same issue. I basically compute the "next" autonum and store it as metadata in my persistent store. If there's not one already stored (only happens once), I compute the "next" one based on the max currently in the database.



- (NSNumber *)nextImageID {
NSPersistentStoreCoordinator * psc = [[self managedObjectContext] persistentStoreCoordinator];
id pStore = [psc persistentStoreForURL:[self fileURL]];
if (pStore) {
NSMutableDictionary * metadata = [[[psc metadataForPersistentStore:pStore] mutableCopy] autorelease];
NSNumber * nextID = [metadata objectForKey:@"NextUnusedImageID"];
if (!nextID) {
nextID = [NSNumber numberWithInt:[[self maxExistingImageID] intValue] + 1];
}
[nextID retain]; // goes out of scope with [psc setMetadata...
[metadata setObject:[NSNumber numberWithInt:[nextID intValue] + 1] forKey:@"NextUnusedImageID"];
[psc setMetadata:metadata forPersistentStore:pStore];
return [nextID autorelease];
} else {
return [NSNumber numberWithInt:[[self maxExistingImageID] intValue] + 1];
}
}



// Return the maximum imageID of all/any records currently in the database.
- (NSNumber *)maxExistingImageID {
NSManagedObjectContext * moc = [self managedObjectContext];
NSEntityDescription * entityDescription = [NSEntityDescription entityForName:@"Image" inManagedObjectContext:moc];
NSFetchRequest * request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:entityDescription];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"imageID" ascending:NO];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];
NSArray * allImages = [moc executeFetchRequest:request error:nil];
NSNumber * itsImageID = [NSNumber numberWithInt:0];
if (allImages && [allImages count] > 0) {
itsImageID = [[allImages objectAtIndex:0] valueForKey:@"imageID"];
}
return itsImageID;
}



HTH, -Dave -- () The ASCII Ribbon Campaign /\ Help Cure HTML Email


Attachment: smime.p7s
Description: S/MIME cryptographic signature

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Core Data and auto increment (From: Eric Morand <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.