radius
- (float) radius { [self willAccessValueForKey: @"radius"]; float f = radius; [self didAccessvalueForKey: @"radius"]; return (f); } // radius - (void) setRadius: (float) newRadius { [self willChangeValueForKey: @"radius"]; radius = newRadius; [self didChangeValueForKey: @"radius"]; } // setRadius
- (NSString *) name { [self willAccessValueForKey: @"name"]; NSString *string = [self primitiveValueForKey: @"name"]; [self didAccessValueForKey: @"name"]; } // name - (void) setName: (NSString *) x { [self willChangeValueForKey: @"name"]; [self setPrimitiveValue: x forKey: @"name"]; [self didChangeValueForKey: @"name"]; } // setName
self
has methods to get the managed object context and managed object model (Apple's CoreData Application template does)
Thanks to Evan Moseman who pointed me at the new easy way to do it:
NSManagedObjectContext *moc = [self managedObjectContext]; NSManagedObject *obj = [NSEntityDescription insertNewObjectForEntityForName :@"Condition" inManagedObjectContext: context];And the older, more manual way if you need some more control over the process:
NSManagedObjectContext *moc = [self managedObjectContext]; NSManagedObjectModel *mom = [self managedObjectModel]; NSEntityDescription *entity; entity = [[mom entitiesByName] objectForKey: @"Condition"]; NSString *className; className = [entity managedObjectClassName]; NSManagedObject *obj; obj = [[NSClassFromString(className) alloc] initWithEntity: entity insertIntoManagedObjectContext: moc]; [obj autorelease]; [obj setValue: @"nook" forKey: @"name"]; [obj setValue: [NSNumber numberWithInt: 23] forKey: @"ranking"];
mutableSetValueForKey:
This returns a proxy that mutates the relationship and does KVO notifications. Think of the name as "[NS]MutableSet" "valueForKey" rather than "mutableSetValue" "forKey", because it returns a mutable set that you manipulate
NSMutableSet *employees; employees = [department mutableSetValueForKey: @"employees"]; [employees addObject: newEmployee]; [employees removeObject: sackedEmployee];
NSSortDescriptor *sorter; sorter = [[NSSortDescriptor alloc] initWithKey: @"when" ascending: YES]; [fetchRequest setSortDescriptors: [NSArray arrayWithObject: sortDescriptor]];
awakeFromInsert
:
- (void) awakeFromInsert { [super awakeFromInsert]; NSDate *date = [NSDate date]; [self setValue: date forKey: @"when"]; } // awakeFromInsert
- (void) setWhenSortDescriptors: (NSArray *) descriptors { } // setWhenSortDescriptors - (NSArray *) whenSortDescriptors { NSSortDescriptor *sorter; sorter = [[[NSSortDescriptor alloc] initWithKey: @"when" ascending: NO] autorelease]; return ([NSArray arrayWithObject: sorter]); } // whenSortDescriptorsThis is for a 'permanent' sorting, not allowing the user to change the sorting. Also, new/changed objects added to the collection don't appear to be placed in sorted order.
Don't use the XML store, but instead use the SQLite store
If an entity has a large data blob as an attribute, you should make a separate entity for that attribute.
e.g. a Person has a 'photo' attribute. Create a Photo entity with a single attribute (the data) and a relationship back to the person (if it makes sense, usually it does), and a relationship from the Person to the Photo entity.
This means that the photo data will only be loaded from the persistent store if you actually use it.
(markd: this also has the nice side effect of letting the db put all the blob data elsewhere, rather than in with the other data, giving you better locality of reference to your Person data without slogging through all the Photo jazz if you don't need it)