Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Commit

Permalink
Refresh destination object when an insert fails, prevent dangling rel…
Browse files Browse the repository at this point in the history
…ationships.
  • Loading branch information
jerryhjones committed Jul 2, 2013
1 parent 6370cf5 commit c296f31
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion AFIncrementalStore/AFIncrementalStore.m
Expand Up @@ -546,7 +546,30 @@ - (id)executeSaveChangesRequest:(NSSaveChangesRequest *)saveChangesRequest
[context refreshObject:insertedObject mergeChanges:NO];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Insert Error: %@", error);
NSLog(@"Insert Error: %@", error);

// Reset destination objects to prevent dangling relationships!
for (NSRelationshipDescription *relationship in [insertedObject.entity.relationshipsByName allValues]) {

NSString *relationshipName = relationship.name;
NSRelationshipDescription *inverseRelationship = relationship.inverseRelationship;
NSArray *destinationObjects = nil;

if (nil == inverseRelationship) {
continue;
}

if ([relationship isToMany]) {
destinationObjects = [insertedObject valueForKey:relationshipName];
} else {
NSManagedObject *destinationObject = [insertedObject valueForKey:relationshipName];
destinationObjects = @[destinationObject];
}

for (NSManagedObject *destinationObject in destinationObjects) {
[context refreshObject:destinationObject mergeChanges:NO];
}
}
}];

[mutableOperations addObject:operation];
Expand Down

0 comments on commit c296f31

Please sign in to comment.