• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Core Data and auto increment
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Core Data and auto increment


  • Subject: Re: Core Data and auto increment
  • From: David Emme <email@hidden>
  • Date: Sat, 29 Jul 2006 07:20:28 -0700


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>)

  • Prev by Date: Re: NSTextField subclass - not using NSTextFieldCell subclass when unarchived from nib?
  • Next by Date: Re: NSTextField subclass - not using NSTextFieldCell subclass when unarchived from nib?
  • Previous by thread: Core Data and auto increment
  • Next by thread: NSTabViewItem objects not responding appropriately to mouse clicks in custom NSTabView
  • Index(es):
    • Date
    • Thread