bug fixes for issues related to error conditions that were preventing our request lifecycle from completing correctly

This commit is contained in:
Jeff Arena
2010-12-02 14:17:58 -08:00
parent c9ba879937
commit 507509fd3e
2 changed files with 62 additions and 21 deletions

View File

@@ -143,18 +143,26 @@ static const NSInteger kMaxConcurrentLoads = 5;
* the completed request from the queue and continue processing
*/
- (void)responseDidLoad:(NSNotification*)notification {
if (notification.object && [notification.object isKindOfClass:[RKResponse class]]) {
RKResponse* response = (RKResponse*)notification.object;
NSError* error = (NSError*)[notification.userInfo objectForKey:@"error"];
if (error) {
NSLog(@"Request failed and removed: URL=%@, error=%@", [[response request] URL], error);
} else {
if (notification.object) {
// Our RKRequest completed and we're notified with an RKResponse object
if ([notification.object isKindOfClass:[RKResponse class]]) {
RKResponse* response = (RKResponse*)notification.object;
NSLog(@"Request completed and removed: URL=%@", [[response request] URL]);
[_requests removeObject:[response request]];
_totalLoading--;
// Our RKRequest failed and we're notified with the original RKRequest object
} else if ([notification.object isKindOfClass:[RKRequest class]]) {
RKRequest* request = (RKRequest*)notification.object;
NSError* error = (NSError*)[notification.userInfo objectForKey:@"error"];
NSLog(@"Request failed and removed: URL=%@, error=%@", [request URL], error);
[_requests removeObject:request];
_totalLoading--;
}
[_requests removeObject:[response request]];
_totalLoading--;
[self loadNextInQueue];
}

View File

@@ -66,27 +66,52 @@
#pragma mark Response Processing
- (void)responseProcessingComplete {
- (void)responseProcessingSuccessful:(BOOL)successful withError:(NSError*)error {
_isLoading = NO;
_isLoaded = YES;
NSDate* receivedAt = [NSDate date];
NSDictionary* userInfo = [NSDictionary dictionaryWithObjectsAndKeys:[self HTTPMethod], @"HTTPMethod", [self URL], @"URL", receivedAt, @"receivedAt", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:kRKResponseReceivedNotification object:_response userInfo:userInfo];
if (successful) {
_isLoaded = YES;
NSDictionary* userInfo = [NSDictionary dictionaryWithObjectsAndKeys:[self HTTPMethod], @"HTTPMethod",
[self URL], @"URL",
receivedAt, @"receivedAt",
nil];
[[NSNotificationCenter defaultCenter] postNotificationName:kRKResponseReceivedNotification
object:_response
userInfo:userInfo];
} else {
NSDictionary* userInfo = [NSDictionary dictionaryWithObjectsAndKeys:[self HTTPMethod], @"HTTPMethod",
[self URL], @"URL",
receivedAt, @"receivedAt",
error, @"error",
nil];
[[NSNotificationCenter defaultCenter] postNotificationName:kRKRequestFailedWithErrorNotification
object:self
userInfo:userInfo];
}
}
- (BOOL)encounteredErrorWhileProcessingRequest:(RKResponse*)response {
if ([response isFailure]) {
[(NSObject<RKObjectLoaderDelegate>*)_delegate objectLoader:self didFailWithError:response.failureError];
[self responseProcessingSuccessful:NO withError:response.failureError];
return YES;
} else if ([response isError]) {
NSError* error = nil;
if ([response isJSON]) {
[(NSObject<RKObjectLoaderDelegate>*)_delegate objectLoader:self didFailWithError:[_mapper parseErrorFromString:[response bodyAsString]]];
error = [_mapper parseErrorFromString:[response bodyAsString]];
[(NSObject<RKObjectLoaderDelegate>*)_delegate objectLoader:self didFailWithError:error];
} else {
if ([_delegate respondsToSelector:@selector(objectLoaderDidLoadUnexpectedResponse:)]) {
[(NSObject<RKObjectLoaderDelegate>*)_delegate objectLoaderDidLoadUnexpectedResponse:self];
}
}
}
[self responseProcessingSuccessful:NO withError:error];
return YES;
}
return NO;
@@ -110,7 +135,7 @@
[(NSObject<RKObjectLoaderDelegate>*)_delegate objectLoader:self didLoadObjects:[NSArray arrayWithArray:objects]];
[self responseProcessingComplete];
[self responseProcessingSuccessful:YES withError:nil];
}
- (void)informDelegateOfObjectLoadErrorWithInfoDictionary:(NSDictionary*)dictionary {
@@ -126,7 +151,7 @@
[(NSObject<RKObjectLoaderDelegate>*)_delegate objectLoader:self didFailWithError:rkError];
[self responseProcessingComplete];
[self responseProcessingSuccessful:NO withError:rkError];
}
@@ -201,6 +226,16 @@
[pool release];
}
- (void)didFailLoadWithError:(NSError*)error {
if ([_delegate respondsToSelector:@selector(request:didFailLoadWithError:)]) {
[_delegate request:self didFailLoadWithError:error];
}
[(NSObject<RKObjectLoaderDelegate>*)_delegate objectLoader:self didFailWithError:error];
[self responseProcessingSuccessful:NO withError:error];
}
- (void)didFinishLoad:(RKResponse*)response {
_response = [response retain];
@@ -217,10 +252,8 @@
if ([_delegate respondsToSelector:@selector(objectLoaderDidLoadUnexpectedResponse:)]) {
[(NSObject<RKObjectLoaderDelegate>*)_delegate objectLoaderDidLoadUnexpectedResponse:self];
}
[self responseProcessingComplete];
[self responseProcessingSuccessful:NO withError:nil];
}
} else {
[self responseProcessingComplete];
}
}