Skip to content

Commit

Permalink
Merge pull request #1700 from novkostya/bugfix/1695
Browse files Browse the repository at this point in the history
Prevents deletion of managed objects if a response mapper error is encountered
  • Loading branch information
blakewatters committed Dec 4, 2013
2 parents ad3750c + b0ee437 commit d930d06
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
10 changes: 6 additions & 4 deletions Code/Network/RKManagedObjectRequestOperation.m
Expand Up @@ -613,10 +613,12 @@ - (void)performMappingOnResponseWithCompletionBlock:(void(^)(RKMappingResult *ma
return completionBlock(nil, error);
}
}

success = [weakSelf deleteLocalObjectsMissingFromMappingResult:mappingResult error:&error];
if (! success || [weakSelf isCancelled]) {
return completionBlock(nil, error);

if (!responseMappingError) {
success = [weakSelf deleteLocalObjectsMissingFromMappingResult:mappingResult error:&error];
if (! success || [weakSelf isCancelled]) {
return completionBlock(nil, error);
}
}

// Persist our mapped objects
Expand Down
53 changes: 53 additions & 0 deletions Tests/Logic/Network/RKManagedObjectRequestOperationTest.m
Expand Up @@ -1510,6 +1510,59 @@ - (void)testThanAnEmptyResponseTriggersDeletionOfOrphanedObjects
expect([human hasBeenDeleted]).to.equal(YES);
}

- (void)testThatErrorStatusCodeDoesNotTriggerDeletionOfOrphanedObjects
{
RKManagedObjectStore *managedObjectStore = [RKTestFactory managedObjectStore];
RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:@"Human" inManagedObjectStore:managedObjectStore];
[entityMapping addAttributeMappingsFromDictionary:@{ @"id": @"railsID", @"name": @"name" }];
entityMapping.identificationAttributes = @[ @"railsID" ];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:entityMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"human" statusCodes:nil];
RKInMemoryManagedObjectCache *managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];
RKHuman *human = [NSEntityDescription insertNewObjectForEntityForName:@"Human" inManagedObjectContext:managedObjectStore.mainQueueManagedObjectContext];
human.railsID = @1;
[managedObjectStore.mainQueueManagedObjectContext saveToPersistentStore:nil];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"/500" relativeToURL:[RKTestFactory baseURL]]];
request.HTTPMethod = @"GET";
RKManagedObjectRequestOperation *managedObjectRequestOperation = [[RKManagedObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];
managedObjectRequestOperation.managedObjectContext = managedObjectStore.mainQueueManagedObjectContext;
managedObjectRequestOperation.managedObjectCache = managedObjectCache;
RKFetchRequestBlock fetchRequestBlock = ^(NSURL *URL){
return [NSFetchRequest fetchRequestWithEntityName:@"Human"];
};
managedObjectRequestOperation.deletesOrphanedObjects = YES;
managedObjectRequestOperation.fetchRequestBlocks = @[ fetchRequestBlock ];
[managedObjectRequestOperation start];
expect([managedObjectRequestOperation isFinished]).will.equal(YES);
expect([human hasBeenDeleted]).to.equal(NO);
}

- (void)testThatCorruptedBodyDoesNotTriggerDeletionOfOrphanedObjects
{
RKManagedObjectStore *managedObjectStore = [RKTestFactory managedObjectStore];
RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:@"Human" inManagedObjectStore:managedObjectStore];
[entityMapping addAttributeMappingsFromDictionary:@{ @"id": @"railsID", @"name": @"name" }];
entityMapping.identificationAttributes = @[ @"railsID" ];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:entityMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"human" statusCodes:nil];
RKInMemoryManagedObjectCache *managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];
RKHuman *human = [NSEntityDescription insertNewObjectForEntityForName:@"Human" inManagedObjectContext:managedObjectStore.mainQueueManagedObjectContext];
human.railsID = @1;
[managedObjectStore.mainQueueManagedObjectContext saveToPersistentStore:nil];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"/corrupted/json" relativeToURL:[RKTestFactory baseURL]]];
request.HTTPMethod = @"GET";
RKManagedObjectRequestOperation *managedObjectRequestOperation = [[RKManagedObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];
managedObjectRequestOperation.managedObjectContext = managedObjectStore.mainQueueManagedObjectContext;
managedObjectRequestOperation.managedObjectCache = managedObjectCache;
RKFetchRequestBlock fetchRequestBlock = ^(NSURL *URL){
return [NSFetchRequest fetchRequestWithEntityName:@"Human"];
};
managedObjectRequestOperation.deletesOrphanedObjects = YES;
managedObjectRequestOperation.fetchRequestBlocks = @[ fetchRequestBlock ];
[managedObjectRequestOperation start];
expect([managedObjectRequestOperation isFinished]).will.equal(YES);
expect([human hasBeenDeleted]).to.equal(NO);
}


- (void)testThatAResponseContainingOnlyNonManagedObjectsTriggersDeletionOfOrphanedObjects
{
RKManagedObjectStore *managedObjectStore = [RKTestFactory managedObjectStore];
Expand Down
6 changes: 6 additions & 0 deletions Tests/Server/server.rb
Expand Up @@ -342,6 +342,12 @@ def render_fixture(path, options = {})
{ :user_ids => [1, 2, 3] }.to_json
end

get '/corrupted/json' do
content_type 'application/json'
status 200
'no json here'
end

# start the server if ruby file executed directly
run! if app_file == $0
end

0 comments on commit d930d06

Please sign in to comment.