2017-03-14 4 views
1

私のアプリケーションの実行は通常、didReceiveChallengeで2〜3秒(5秒)停止します。 10回のうち1回は永遠にかかる。 すべてが機能しますが、スピードアップするにはどうすればよいですか?URLSession didReceiveChallengeが遅すぎる

は、ここに私のコードです:

- (void)URLSession:(NSURLSession *)session 
didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge 
completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * __nullable credential))completionHandler{ 
    NSLog(@"*** KBRequest.NSURLSessionDelegate - didReceiveChallenge IOS10"); 
    if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){ 
     if([challenge.protectionSpace.host isEqualToString:@"engine.my.server"]){ 
      NSURLCredential *credential = [NSURLCredential credentialForTrust: challenge.protectionSpace.serverTrust]; 
      completionHandler(NSURLSessionAuthChallengeUseCredential,credential); 
     } 
     else{ 
      completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil); 
     } 
    } 
} 

答えて

0

あなたが完了ハンドラにこのメソッドが呼び出されるたびに呼び出す必要があります。あなたは1つの保護スペースのためにそれを呼び出すだけです。

OSがデリゲートでこのメソッドを呼び出すと、NSURLSessionスタックがそこに座って、完了ハンドラブロックを呼び出すのを忠実に待ちます。補完ハンドラを呼び出すことに失敗した場合、リクエストがタイムアウトするまで、あなたのリクエストはちょうどリムボーに座ります。

は法の下では、この問題を解決するには、追加:

} else { 
    completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil); 
}