ios - Is this the right way of using NSOperation (AFXMLRequestOperation)? -
in app have series of background tasks performed 1 after other (say tasks : a, b , c). each of these tasks talk different web services (xml). using afxmlrequestoperation of afnetworking library initiate request web service , handling parsing logic @ success block.
each following task dependent on successful completion of previous task. also, want following task called after delay of few seconds after successful completion of previous. once task c completes successfully, i'm done.
all of happening in background thread, , hence ui thread responsive throughout (my uiactivityindicator keeps moving throughout each tasks separately).
here's pseudo code snippet:
-(void)viewdidload { _operationqueue = [[nsoperationqueue alloc]init]; [_operationqueue setmaxconcurrentoperationcount:1]; [self taska]; } - taska { nsmutableurlrequest *request = urla; afxmlrequestoperation *operationa = [afxmlrequestoperation xmlparserrequestoperationwithrequest:request success:^(nsurlrequest *request, nshttpurlresponse *response, nsxmlparser *xmlparser) { xmlparser.delegate = someclass; [xmlparser parse]; // since operation successful, start task b after delay of 5 seconds [self performselector:@selector(taskb) withobject:nil afterdelay:5]; failure:^(nsurlrequest *request, nshttpurlresponse *response, nserror *error, nsxmlparser *xmlparser) { nslog(@"nserror: %@",error); }]; [_operationqueue addoperation: operationa]; } - taskb { nsmutableurlrequest *request = urlb; afxmlrequestoperation *operationb = [afxmlrequestoperation xmlparserrequestoperationwithrequest:request success:^(nsurlrequest *request, nshttpurlresponse *response, nsxmlparser *xmlparser) { xmlparser.delegate = someotherclass; [xmlparser parse]; // since operation successful, start task c after 10 seconds [self performselector:@selector(taskc) withobject:nil afterdelay:10]; failure:^(nsurlrequest *request, nshttpurlresponse *response, nserror *error, nsxmlparser *xmlparser) { }]; [_operationqueue addoperation: operationb]; [operationb adddependency:operationa]; // code seems produce no result , hence seems redundant } - taskc { nsmutableurlrequest *request = urlb; afxmlrequestoperation *operationc = [afxmlrequestoperation xmlparserrequestoperationwithrequest:request success:^(nsurlrequest *request, nshttpurlresponse *response, nsxmlparser *xmlparser) { xmlparser.delegate = yetanotherclass; [xmlparser parse]; // since operation successful, mission accomplished! nslog(@"mission accomplished!"); failure:^(nsurlrequest *request, nshttpurlresponse *response, nserror *error, nsxmlparser *xmlparser) { }]; [_operationqueue addoperation: operationb]; [operationc adddependency:operationb]; // code seems produce no result , hence seems redundant }
questions:
i'm able achieve wanted app implementation, i'm not sure if i'm making right use of nsoperation , nsoperationqueue. of read apple docs , tutorials, 1 of strengths of nsoperation using dependency establishment between different operations. however, in example how can ensure operationb gets executed after 'successful' completion of task , leverage "adddependency" feature of nsoperation?
i want ensure 'taskb' gets called after delay after successful completion of 'taska' , on.
[self performselector:@selector(taskb) withobject:nil afterdelay:5];
the way it? or there alternative ways, use elements of nsoperation/nsoperationqueues? or maybe use "dispatch_after"??
overall, how can redesign code better same tasks accomplished using nsoperation?
Comments
Post a Comment