Core Data - awakeFromInsert
Core Data - awakeFromInsert
- Subject: Core Data - awakeFromInsert
- From: Jim Murry <email@hidden>
- Date: Tue, 4 Oct 2005 16:43:12 -0700
I am trying to implement defaults by creating a core data object to
hold these defaults. This way they do not reset to zero when the
program is run from a different user or a different machine as long
as they have access to the same core data file. Two entries are
created using the following code and I don't know why. It seems to
occur at executeFetchRequest.
Thank You.
#import "Customer.h"
@implementation Customer
-(void) awakeFromInsert
{
// The following works for incrementing ID's for "same user" on
"same machine", it resets to zero for different user or machine. ONLY
ONE ENTRY CREATED
// long value = [[[NSUserDefaults standardUserDefaults]
objectForKey: @"recordID"] longValue];
// [[NSUserDefaults standardUserDefaults] setValue:[NSNumber
numberWithLong:(value + 1)] forKey:@"recordID"];
// [self setPrimitiveValue:[NSNumber numberWithLongLong:
(value)] forKey:@"recordID"];
// The following works for incrementing ID's for "any user" on
"any machine", but it creates 2 entries each time.
NSFetchRequest *request = [[[NSFetchRequest alloc] init]
autorelease];
NSEntityDescription *entity = [[[[NSApp delegate]
managedObjectModel] entitiesByName] objectForKey:@"Defaults"];
[request setEntity:entity];
NSManagedObjectContext *context = [[NSApp delegate]
managedObjectContext];
// If I comment out everything below only one entry is created.
NSArray *myArray = [context executeFetchRequest:request
error:nil]; // This line creates the problem.
id masterRecord = [myArray objectAtIndex:0];
long value = [[masterRecord valueForKeyPath: @"recordID"]
longValue];
[masterRecord willChangeValueForKey: @"recordID"];
[masterRecord setPrimitiveValue:[NSNumber numberWithLongLong:
(value + 1)] forKey:@"recordID"];
[masterRecord didChangeValueForKey: @"recordID"];
[self setPrimitiveValue:[NSNumber numberWithLongLong:(value)]
forKey:@"recordID"];
}
@end
_______________________________________________
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