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

凉白开 2012-04-09

SELdidStartSelector;

 SEL didReceiveResponseHeadersSelector;

 SEL willRedirectSelector;

 SEL didFinishSelector;

SELdidFailSelector;

 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
}

相关推荐

TiDBPingCAP / 0评论 2020-07-29