ios - When subclassing RKObjectRequestOperation to check for errors, how to retry the failed operation? -


i have subclassed rkmanagedobjectrequestoperation , overriding following method,

    - (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;         }     }]; } 

then in observer of failure notification, issue request refresh oauth token , on success, want enqueue original request again, after substituting querystring parameter original operation.

in code notification observer:

-(void)restrequestfailedwithoperation:(nsnotification*)notification{      rkmanagedobjectrequestoperation *operation = (rkmanagedobjectrequestoperation *)notification.object;     if (operation) {                  // issue token refresh request , in success block:                 .....                    [[rkobjectmanager sharedmanager] enqueueobjectrequestoperation:operation];           }       } 

i did not see means replace specific querystring parameter within rkmanagedobjectrequestoperation. , since original operation still has old token, enqueueing again not work.

one workaround move concern of appending oauth tokens each outgoing request subclass rkhttprequestoperation class , override method

-(nsurlrequest*)connection:(nsurlconnection *)connection willsendrequest:(nsurlrequest *)request redirectresponse:(nsurlresponse *)response{  } 

this ensure each outgoing request current oauth token. gets away needing modify failed operation new request.

make sure register subclass(myhttprequestoperation) this:

[[rkobjectmanager sharedmanager] registerrequestoperationclass:[myhttprequestoperation class]]; 

i not confident best approach. ideally, pattern set rkpaginator class implemented. rkpaginator allows easy repeat of same request while modifying query parameters.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -