Permalink
Browse files

MTLManagedObjectAdapter: Fixes an issue with validate for insert

Fixes an issue where validation for insert would return a deleted
managed object without an error

When it does this in combination with
`relationshipModelClassesByPropertyKey` CoreData could raise an
exception for using setting child’s in a different context (because it
doesn’t have a context)
  • Loading branch information...
Kyle Fuller
Kyle Fuller committed Jan 27, 2014
1 parent 13a39ca commit 027e8c1c4ccb15665f7899729b00c64f0b5212ec
@@ -502,8 +502,8 @@ - (id)managedObjectFromModel:(MTLModel<MTLManagedObjectSerializing> *)model inse
}
}];
- if (managedObject != nil && ![managedObject validateForInsert:error]) {
- performInContext(context, ^ id {
+ if (managedObject != nil && ![managedObject validateForInsert:&tmpError]) {
+ managedObject = performInContext(context, ^ id {
[context deleteObject:managedObject];
return nil;
});
@@ -23,6 +23,11 @@
@end
+// Model for Parent entity which doesn't serialize required properties
+@interface MTLParentIncorrectTestModel : MTLModel <MTLManagedObjectSerializing>
+
+@end
+
// Corresponds to the `Child` entity.
@interface MTLChildTestModel : MTLModel <MTLManagedObjectSerializing>
@@ -42,6 +42,18 @@ + (NSDictionary *)relationshipModelClassesByPropertyKey {
@end
+@implementation MTLParentIncorrectTestModel
+
++ (NSString *)managedObjectEntityName {
+ return @"Parent";
+}
+
++ (NSDictionary *)managedObjectKeysByPropertyKey {
+ return @{};
+}
+
+@end
+
@implementation MTLChildTestModel
+ (NSString *)managedObjectEntityName {
@@ -226,6 +226,16 @@
expect(error).notTo.beNil();
});
+ it(@"should return an error if model doesn't validate for insert", ^{
+ MTLParentIncorrectTestModel *parentModel = [MTLParentIncorrectTestModel modelWithDictionary:@{} error:NULL];
+
+ NSError *error;
+ NSManagedObject *managedObject = [MTLManagedObjectAdapter managedObjectFromModel:parentModel insertingIntoContext:context error:&error];
+
+ expect(managedObject).to.beNil();
+ expect(error).notTo.beNil();
+ });
+
it(@"should respect the uniqueness constraint", ^{
NSError *errorOne;
MTLParent *parentOne = [MTLManagedObjectAdapter managedObjectFromModel:parentModel insertingIntoContext:context error:&errorOne];

0 comments on commit 027e8c1

Please sign in to comment.