From 6146607dea7820b7f5cdd1231e7562cf60eb463f Mon Sep 17 00:00:00 2001 From: Okhan Okbay Date: Mon, 5 Aug 2024 15:01:49 +0100 Subject: [PATCH] Update the code --- .../Tokenisation/CheckoutAPIService.swift | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/Checkout/Source/Tokenisation/CheckoutAPIService.swift b/Checkout/Source/Tokenisation/CheckoutAPIService.swift index ad2fab0b..7cd6d59f 100644 --- a/Checkout/Source/Tokenisation/CheckoutAPIService.swift +++ b/Checkout/Source/Tokenisation/CheckoutAPIService.swift @@ -146,8 +146,8 @@ final public class CheckoutAPIService: CheckoutAPIProtocol { } let timeoutInterval: TimeInterval = 10 - let taskCompletionQueue = DispatchQueue(label: "taskCompletionQueue") - var isTaskCompleted = false + private let taskCompletionQueue = DispatchQueue(label: "taskCompletionQueue") + private var isTaskCompleted = false private func createToken(requestParameters: NetworkManager.RequestParameters, paymentType: TokenRequest.TokenType, @@ -168,24 +168,14 @@ final public class CheckoutAPIService: CheckoutAPIProtocol { return } - DispatchQueue.global().asyncAfter(deadline: .now() + timeoutInterval) { - self.taskCompletionQueue.sync { [weak self] in - guard let self else { return } - if !isTaskCompleted { - isTaskCompleted = true - completion(.failure(.networkError(.connectionTimeout))) - } - } - } - - callRiskSDK(tokenDetails: tokenDetails) { [weak self] result in + self.callRiskSDK(tokenDetails: tokenDetails) { [weak self] in guard let self else { return } - taskCompletionQueue.sync { + self.taskCompletionQueue.sync { if !self.isTaskCompleted { self.isTaskCompleted = true + completion(.success(tokenDetails)) } } - completion(result) } case .errorResponse(let errorResponse): @@ -199,18 +189,27 @@ final public class CheckoutAPIService: CheckoutAPIProtocol { } private func callRiskSDK(tokenDetails: TokenDetails, - completion: @escaping (Result) -> Void) { - DispatchQueue.global().async { + completion: @escaping () -> Void) { + + DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + timeoutInterval) { + self.taskCompletionQueue.sync { [weak self] in + guard let self else { return } + if !self.isTaskCompleted { + self.isTaskCompleted = true + completion() + } + } + self.riskSDK.configure { [weak self] configurationResult in guard let self else { return } switch configurationResult { case .failure: - completion(.success(tokenDetails)) + completion() logManager.resetCorrelationID() case .success(): self.riskSDK.publishData(cardToken: tokenDetails.token) { _ in self.logManager.queue(event: .riskSDKCompletion) - completion(.success(tokenDetails)) + completion() self.logManager.resetCorrelationID() } }