ios - How exactly does AFNetworking work? -
hi all, i've played asihttprequest
, afnetworking while, today, try read source code of afnetworking more deeply, it's powerful , designed network framework, got several questions ask: 1, afjsonrequestoperation
:
nsmutableurlrequest *request = [httpclient requestwithmethod:@"post" path:kmobilebind parameters:paramsbase]; afjsonrequestoperation *operation = [afjsonrequestoperation jsonrequestoperationwithrequest:request success:^(nsurlrequest *request, nshttpurlresponse *response, id json) { //successblock } fail:^(nsurlrequest *request, nshttpurlresponse *response, nserror *error, id json){ //failblock }
after this, operation created , success block , fail block set it, start operation, starts nsurlconnection
in singleton thread,
[self performselector:@selector(operationdidstart) onthread:[[self class] networkrequestthread] withobject:nil waituntildone:no modes:[self.runloopmodes allobjects]];
inside operationdidstart:
[self.lock lock]; nsrunloop *runloop = [nsrunloop currentrunloop]; (nsstring *runloopmode in self.runloopmodes) { [self.connection scheduleinrunloop:runloop formode:runloopmode]; [self.outputstream scheduleinrunloop:runloop formode:runloopmode]; } [self.connection start]; [self.lock unlock];
this operation runs , nsurlconnection
callbacks called,
- (void)connectiondidfinishloading:(nsurlconnection *)__unused connection { self.responsedata = [self.outputstream propertyforkey:nsstreamdatawrittentomemorystreamkey]; [self.outputstream close]; [self finish]; self.connection = nil; } - (void)finish { self.state = afhttpoperationfinishedstate; }
the finish method set state of operation, , uses kvo tech, finished operation: didn't see observevalueforkeypath , [obj addobserver:self forkeypath:@"isfinished" options:0 context:null]; how kvo work?
setstate:
[self willchangevalueforkey:newstatekey]; [self willchangevalueforkey:oldstatekey]; _state = state; [self didchangevalueforkey:oldstatekey]; [self didchangevalueforkey:newstatekey]; - (bool)isfinished { return self.state == afhttpoperationfinishedstate; }
then got stuck here, find setcallblock fund, can't understant how self.completionblock called:
- (void)setcompletionblockwithsuccess:(void (^)(afhttprequestoperation *operation, id responseobject))success failure:(void (^)(afhttprequestoperation *operation, nserror *error))failure { self.completionblock = ^ { if ([self iscancelled]) { return; } if (self.error) { if (failure) { dispatch_async(self.failurecallbackqueue ? self.failurecallbackqueue : dispatch_get_main_queue(), ^{ failure(self, self.error); }); } } else { if (success) { dispatch_async(self.successcallbackqueue ? self.successcallbackqueue : dispatch_get_main_queue(), ^{ success(self, self.responsedata); }); } } };
}
may did read code carefully, gets me confused, 1 has experience on afnetworking pls explains framework,
one more question, network requests ran on shared thread, slow down request?
i wanna improve skill in mutilthread programming, , need samples , suggestions, i've read apple's docs, , made simple demos, haven't tried on big project or code robust framework or model, how start? many
completionblock
property of afjsonrequestoperation
's superclass, nsoperation
. executed when operation completes.
Comments
Post a Comment