mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-24 04:46:01 +08:00
Add support for deleting Core Data managed objects that fail validation out of the mapping context. This enables you to silently drop mapping for managed objects that fail validation. fixes #691 closes #694
This commit is contained in:
@@ -32,6 +32,7 @@
|
||||
#import "RKValueTransformers.h"
|
||||
#import "RKRelationshipMapping.h"
|
||||
#import "RKObjectUtilities.h"
|
||||
#import "NSManagedObject+RKAdditions.h"
|
||||
|
||||
extern NSString * const RKObjectMappingNestingAttributeKeyName;
|
||||
|
||||
@@ -243,8 +244,7 @@ extern NSString * const RKObjectMappingNestingAttributeKeyName;
|
||||
}
|
||||
|
||||
if (managedObject == nil) {
|
||||
managedObject = [[NSManagedObject alloc] initWithEntity:entity
|
||||
insertIntoManagedObjectContext:self.managedObjectContext];
|
||||
managedObject = [[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:self.managedObjectContext];
|
||||
[managedObject setValuesForKeysWithDictionary:entityIdentifierAttributes];
|
||||
|
||||
if ([self.managedObjectCache respondsToSelector:@selector(didCreateObject:)]) {
|
||||
@@ -276,6 +276,16 @@ extern NSString * const RKObjectMappingNestingAttributeKeyName;
|
||||
if ([mappingOperation.objectMapping isKindOfClass:[RKEntityMapping class]]) {
|
||||
[self emitDeadlockWarningIfNecessary];
|
||||
|
||||
// Validate unsaved objects
|
||||
if ([mappingOperation.destinationObject isKindOfClass:[NSManagedObject class]] && [(NSManagedObject *)mappingOperation.destinationObject isNew]) {
|
||||
NSError *validationError = nil;
|
||||
if (! [(NSManagedObject *)mappingOperation.destinationObject validateForInsert:&validationError]) {
|
||||
RKLogDebug(@"Unsaved NSManagedObject failed `validateForInsert:` - Deleting object from context: %@", validationError);
|
||||
[self.managedObjectContext deleteObject:mappingOperation.destinationObject];
|
||||
return YES;
|
||||
}
|
||||
}
|
||||
|
||||
NSArray *connections = [(RKEntityMapping *)mappingOperation.objectMapping connections];
|
||||
if ([connections count] > 0 && self.managedObjectCache == nil) {
|
||||
NSDictionary *userInfo = @{ NSLocalizedDescriptionKey: @"Cannot map an entity mapping that contains connection mappings with a data source whose managed object cache is nil." };
|
||||
|
||||
Reference in New Issue
Block a user