我将 RKManagedObjectRequestOperation 子类化并覆盖了以下方法,
- (void)setCompletionBlockWithSuccess:(void ( ^ ) ( RKObjectRequestOperation *operation , RKMappingResult *mappingResult ))success failure:(void ( ^ ) ( RKObjectRequestOperation *operation , NSError *error ))failure
{
[super setCompletionBlockWithSuccess:^void(RKObjectRequestOperation *operation , RKMappingResult *mappingResult) {
if (success) {
success(operation, mappingResult);
}
}failure:^void(RKObjectRequestOperation *operation , NSError *error) {
NSInteger statusCode = operation.HTTPRequestOperation.response.statusCode;
switch (statusCode) {
case 401: // not authenticated
{
MyError* errorResponse = (MyError*)[[error.userInfo objectForKey:@"RKObjectMapperErrorObjectsKey"] firstObject];
if(errorResponse && [errorResponse.oauth2ErrorCode isEqualToString:@"invalid_token"]){
RKObjectRequestOperation* repeatOp = [operation copy];
[repeatOp setCompletionBlockWithSuccess:success failure:failure];
[[NSNotificationCenter defaultCenter] postNotificationName:@"InvalidTokenFailure" object:repeatOp];
} else{
if (failure) {
failure(operation, error);
}
}
}
break;
default:
{
if (failure) {
failure(operation, error);
}
}
break;
}
}];
}
然后在失败通知的观察者中,我发出刷新 oauth 令牌的请求,成功后,我想再次将原始请求排入队列,但在替换原始操作中的查询字符串参数之后。
在通知观察者的代码中:
-(void)restRequestFailedWithOperation:(NSNotification*)notification{
RKManagedObjectRequestOperation *operation = (RKManagedObjectRequestOperation *)notification.object;
if (operation) {
// issue token refresh request and in its success block:
.....
[[RKObjectManager sharedManager] enqueueObjectRequestOperation:operation];
}
}
我没有看到在 RKManagedObjectRequestOperation 中替换特定查询字符串参数的方法。并且由于原始操作仍然有旧令牌,因此再次将其入队是行不通的。