25 #import "CoreDataHeaders.h"
34 IsCollection(
id object)
36 if ([
object isKindOfClass: [NSSet
class]] ||
37 [
object isKindOfClass: [NSArray
class]])
56 ConstructComplexError(NSError ** target, NSArray * errors)
58 unsigned int errorCount = [errors count];
62 *target = [errors objectAtIndex: 0];
64 else if (errorCount > 1)
67 NSDictionary * userInfo;
69 userInfo = [NSDictionary
70 dictionaryWithObject: [[errors copy] autorelease]
71 forKey: NSDetailedErrorsKey];
72 newError = [NSError errorWithDomain: NSCoreDataErrorDomain
73 code: NSValidationMultipleErrorsError
241 + (BOOL) automaticallyNotifiesObserversForKey: (NSString *) aKey
248 TEST_RELEASE(_entity);
249 TEST_RELEASE(_objectID);
250 TEST_RELEASE(_changedValues);
265 - (id) initWithEntity: (NSEntityDescription *) entity
266 insertIntoManagedObjectContext: (NSManagedObjectContext *) ctxt
268 if ((
self = [super init]))
272 [NSException raise: NSInvalidArgumentException
273 format: _(@"Tried to initialize a managed object "
274 @"from an abstract entity (%@)."),
280 [ctxt insertObject: self];
298 - (NSEntityDescription *)
entity
311 if (_objectID == nil)
325 return [[_context insertedObjects] containsObject: self];
335 return [[_context updatedObjects] containsObject: self];
372 - (NSDictionary *) changedValues
374 return [[_changedValues copy] autorelease];
383 - (void) didTurnIntoFault
389 - (id) valueForKey: (NSString *) key
394 [
self _validatedPropertyForKey: key];
396 [
self willAccessValueForKey: key];
397 value = [
self _primitiveValueForKey: key doValidation: NO];
398 [
self didAccessValueForKey: key];
407 - (void) setValue: (
id) value
408 forKey: (NSString *) key
410 NSPropertyDescription * property;
412 property = [
self _validatedPropertyForKey: key];
413 if ([
self _validateValue: &value
416 property: property] == NO)
418 [NSException raise: NSInvalidArgumentException
419 format: _(@"Invalid value for key %@ specified."), key];
422 [
self willChangeValueForKey: key];
423 [
self _setPrimitiveValue: value forKey: key doValidation: NO];
424 [
self didChangeValueForKey: key];
430 - (id) primitiveValueForKey: (NSString *) key
432 return [
self _primitiveValueForKey: key doValidation: YES];
438 - (void) setPrimitiveValue: (
id) value
439 forKey: (NSString *) key
444 [
self _setPrimitiveValue: value forKey: key doValidation: YES];
449 - (BOOL) validateValue: (
id *) value
450 forKey: (NSString *) key
451 error: (NSError **) error
453 return [
self _validateValue: value
456 property: [
self _validatedPropertyForKey: key]];
465 - (BOOL) validateForDelete: (NSError **) error
468 NSRelationshipDescription * rel;
469 NSMutableArray * errors = [NSMutableArray array];
471 e = [[
self _allPropertiesOfSubclass: [NSRelationshipDescription class]]
473 while ((rel = [e nextObject]) != nil)
475 NSString * key = [rel name];
476 id value = [
self _primitiveValueForKey: key doValidation: NO];
478 if ([rel deleteRule] == NSDenyDeleteRule &&
479 (([rel isToMany] && value != nil && [value count] != 0) ||
488 NSError * localError;
489 NSDictionary * userInfo;
491 userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
492 value, NSValidationValueErrorKey,
493 key, NSValidationKeyErrorKey,
495 localError = [NSError
496 errorWithDomain: NSCoreDataErrorDomain
497 code: NSValidationRelationshipDeniedDeleteError
500 [errors addObject: localError];
505 if ([errors count] > 0)
507 ConstructComplexError(error, errors);
517 - (BOOL) validateForInsert: (NSError **) error
524 - (BOOL) validateForUpdate: (NSError **) error
527 NSPropertyDescription * property;
528 NSMutableArray * errors = [NSMutableArray array];
530 e = [[
self _allPropertiesOfSubclass: [NSPropertyDescription class]]
532 while ((property = [e nextObject]) != nil)
534 NSString * key = [property name];
535 id value = [
self _primitiveValueForKey: key
537 NSError * localError;
539 if ([
self _validateValue: &value
542 property: property] == NO)
551 [errors addObject: localError];
556 if ([errors count] > 0)
558 ConstructComplexError(error, errors);
570 - (void) didAccessValueForKey: (NSString *) key
574 - (void) didChangeValueForKey: (NSString *) key
576 [
super didChangeValueForKey: key];
579 - (void) didChangeValueForKey: (NSString *) key
580 withSetMutation: (NSKeyValueSetMutationKind) mutationKind
581 usingObjects: (NSSet *) objects
583 [
super didChangeValueForKey: key
584 withSetMutation: mutationKind
585 usingObjects: objects];
588 - (
void *) observationInfo
590 return [
super observationInfo];
593 - (void) setObservationInfo: (
void *) info
595 [
super setObservationInfo: info];
598 - (void) willAccessValueForKey: (NSString *) key
602 - (void) willChangeValueForKey: (NSString *) key
604 [
super willChangeValueForKey: key];
607 - (void) willChangeValueForKey: (NSString *) key
608 withSetMutation: (NSKeyValueSetMutationKind) mutationKind
609 usingObjects: (NSSet *) objects
611 [
super willChangeValueForKey: key
612 withSetMutation: mutationKind
613 usingObjects: objects];
623 - (id) _initAsFaultWithEntity: (NSEntityDescription *) entity
624 ownedByContext: (NSManagedObjectContext *) context
626 if ((
self = [super init]))
630 [NSException raise: NSInvalidArgumentException
631 format: _(@"Tried to initialize a managed object "
632 @"from an abstract entity (%@)."),
654 NSAssert([newID isTemporaryID] == NO, _(
@"Tried to assign to a managed "
655 @"object a temporary object ID."));
657 ASSIGN(_objectID, newID);
664 - (void) _setDeleted: (BOOL) flag
673 - (void) _setFault: (BOOL) flag
683 - (void) _insertedIntoContext: (NSManagedObjectContext *) ctxt
685 NSAssert(_context == nil || _context == ctxt, _(
@"Tried to re-insert a "
686 @"managed object into different managed object context."));
699 - (void) _removedFromContext
701 NSAssert(_context != nil, _(
@"Attempted to remove from a context an "
702 @"already removed managed object."));
713 - (NSPropertyDescription *) _validatedPropertyForKey: (NSString *) key
715 NSPropertyDescription * desc = nil;
716 NSEntityDescription *
entity;
721 desc == nil &&
entity != nil;
722 entity = [entity superentity])
724 desc = [[entity propertiesByName] objectForKey: key];
733 [NSException raise: NSInvalidArgumentException
734 format: _(@"Invalid key specified. The key does not "
735 @"exist in the model.")];
745 - (NSArray *) _allPropertiesOfSubclass: (Class) aClass
747 NSMutableArray * properties;
748 NSEntityDescription *
entity;
750 NSAssert(aClass != nil, _(
@"Nil class argument."));
752 properties = [NSMutableArray array];
757 NSPropertyDescription * property;
759 e = [[entity properties] objectEnumerator];
760 while ((property = [e nextObject]) != nil)
762 if ([property isKindOfClass: aClass])
764 [properties addObject: property];
769 return [[properties copy] autorelease];
784 - (BOOL) _validateValue: (
id *) val
785 forKey: (NSString *) key
786 error: (NSError **) error
787 property: (NSPropertyDescription *) property
790 SEL customValidationSel;
822 if ([property isOptional] == NO)
824 SetNonNullError(error, [NSError
825 errorWithDomain: NSCoreDataErrorDomain
826 code: NSValidationMissingMandatoryPropertyError
827 userInfo: [NSDictionary dictionaryWithObjectsAndKeys:
828 self, NSValidationObjectErrorKey,
829 key, NSValidationKeyErrorKey,
837 customValidationSel = NSSelectorFromString([NSString stringWithFormat:
838 @"validate%@:error:", key]);
839 if ([
self respondsToSelector: customValidationSel])
842 NSInvocation * invocation = [[NSInvocation new] autorelease];
844 [invocation setTarget: self];
845 [invocation setSelector: customValidationSel];
846 [invocation setArgument: &value
848 [invocation setArgument: &error
851 [invocation getReturnValue: &retval];
867 - (id) _primitiveValueForKey: (NSString *) key doValidation: (BOOL) validate
871 [
self _validatedPropertyForKey: key];
878 return [_data objectForKey: key];
890 - (void) _setPrimitiveValue: (
id) value
891 forKey: (NSString *) key
892 doValidation: (BOOL) validate
894 NSPropertyDescription * property;
897 property = [
self _validatedPropertyForKey: key];
902 if ([
self _validateValue: &value
905 property: property] != YES)
907 [NSException raise: NSInvalidArgumentException
908 format: _(@"Invalid value for key \"%@\" specified."),
918 [_data setObject: value forKey: key];
920 if ([property isTransient] == NO)
922 if (_changedValues == nil)
924 _changedValues = [NSMutableDictionary new];
926 [_changedValues setObject: value forKey: key];
For implementation notes see "Documentation/NSManagedObjectID.txt" in the source distribution of the ...
Validates whether value'' is a valid value forattribute'', returning YES if it is,...
NSManagedObjectContext * managedObjectContext()
Returns the managed object context to which the receiver belongs.
void awakeFromInsert()
Invoked automatically after the receiver has been inserted into a managed object context.
BOOL isUpdated()
Returns YES if the receiver has changes that have not yet been written to a persistent store (the rec...
BOOL isFault()
Returns YES if the receiver is a fault, and NO otherwise.
void awakeFromFetch()
Invoked automatically after the receiver has been fetched from a persistent store.
NSEntityDescription * entity()
Returns the entity of the receiver.
BOOL isInserted()
Returns YES if the receiver is inserted in a managed object context, and NO otherwise.
BOOL isDeleted()
Returns YES if the receiver has been scheduled in it's parent managed object context for deletion fro...
NSManagedObjectID * objectID()
Returns the object ID of the receiver.