asihttp 源码分析 之三 startRequest 各种回调


 SEL didStartSelector;
 SEL didReceiveResponseHeadersSelector;

 SEL willRedirectSelector;

 SEL didFinishSelector;
 SEL didFailSelector;
 SEL didReceiveDataSelector;

 

各种SEL 的调用时机。

1: SEL didStartSelector;

- (void)startRequest 一开始就调用了requestStarted。

[self performSelectorOnMainThread:@selector(requestStarted) withObject:nil waitUntilDone:[NSThread isMainThread]];

 

/* ALWAYS CALLED ON MAIN THREAD! */
- (void)requestStarted
{
 if ([self error] || [self mainRequest]) {
  return;
 }
 if (delegate && [delegate respondsToSelector:didStartSelector]) {
  [delegate performSelector:didStartSelector withObject:self];
 }
 if (queue && [queue respondsToSelector:@selector(requestStarted:)]) {
  [queue performSelector:@selector(requestStarted:) withObject:self];
 }
 #if NS_BLOCKS_AVAILABLE
 if(startedBlock){
  startedBlock();
 }
 #endif
}

 

2: SEL didReceiveResponseHeadersSelector;

 

/* ALWAYS CALLED ON MAIN THREAD! */
- (void)requestReceivedResponseHeaders:(NSMutableDictionary *)newResponseHeaders
{
 if ([self error] || [self mainRequest]) {
  return;
 }

 if (delegate && [delegate respondsToSelector:didReceiveResponseHeadersSelector]) {
  [delegate performSelector:didReceiveResponseHeadersSelector withObject:self withObject:newResponseHeaders];
 }
 if (queue && [queue respondsToSelector:@selector(request:didReceiveResponseHeaders:)]) {
  [queue performSelector:@selector(request:didReceiveResponseHeaders:) withObject:self withObject:newResponseHeaders];
 }
    
 #if NS_BLOCKS_AVAILABLE
 if(headersReceivedBlock){
  headersReceivedBlock(newResponseHeaders);
    }
 #endif
}

 

 

3: SEL willRedirectSelector;

/* ALWAYS CALLED ON MAIN THREAD! */
- (void)requestWillRedirectToURL:(NSURL *)newURL
{
 if ([self error] || [self mainRequest]) {
  return;
 }
 if (delegate && [delegate respondsToSelector:willRedirectSelector]) {
  [delegate performSelector:willRedirectSelector withObject:self withObject:newURL];
 }
 if (queue && [queue respondsToSelector:@selector(request:willRedirectToURL:)]) {
  [queue performSelector:@selector(request:willRedirectToURL:) withObject:self withObject:newURL];
 }
}

 

 

SEL requestFinished

 

 

/* ALWAYS CALLED ON MAIN THREAD! */
- (void)reportFinished
{
 if (delegate && [delegate respondsToSelector:didFinishSelector]) {
  [delegate performSelector:didFinishSelector withObject:self];
 }
 if (queue && [queue respondsToSelector:@selector(requestFinished:)]) {
  [queue performSelector:@selector(requestFinished:) withObject:self];
 }
#if NS_BLOCKS_AVAILABLE
 if(completionBlock){
  completionBlock();
 }
#endif
}

 

SEL requestFailed

 

/* ALWAYS CALLED ON MAIN THREAD! */
- (void)reportFailure
{
 if (delegate && [delegate respondsToSelector:didFailSelector]) {
  [delegate performSelector:didFailSelector withObject:self];
 }
 if (queue && [queue respondsToSelector:@selector(requestFailed:)]) {
  [queue performSelector:@selector(requestFailed:) withObject:self];
 }
 #if NS_BLOCKS_AVAILABLE
    if(failureBlock){
        failureBlock();
    }
 #endif
}

 

 

 

你可能感兴趣的:(request)