gpt4 book ai didi

Swift:减慢对 WebService 的多次调用

转载 作者:行者123 更新时间:2023-11-28 15:51:08 26 4
gpt4 key购买 nike

我正在开发一个向网络服务发送多个请求的应用程序。随着我进一步开发,我发现 Web 服务变得不堪重负,我需要放慢速度。我想将每个请求单独发送到 Web 服务,并等到前一个请求完成后再发送下一个请求。下面是使用循环调用 Web 服务的函数:

func syncronize(){

for operation in syncOperations{
switch operation{
case "listPhone":
let listRequest = WSListRequest(requestType: operation, searchCriteria: [SearchCriteria(name: "name", value: "%")], returnTags: [])
_ = HTTPPost(method: "POST", body: listRequest.xml, operation: operation, credentials: creds)
default:
let listRequest = WSLListRequest(requestType: operation, searchCriteria: [SearchCriteria(name: "name", value: "%")], returnTags: ["name"])
_ = HTTPPost(method: "POST", body: listRequest.xml, operation: operation, credentials: creds)
}
}
}

HTTPPost 函数如下所示:

class HTTPPost: NSObject, URLSessionDelegate {

var componentDebug = false
var user = String()
var password = String()
var server = String()
var port = String()
var body = NSString()
var response = Data()

init(method: String, body: NSString, operation: String, credentials: WSCredential){
super.init()
let bodyData = body.data(using: String.Encoding.utf8.rawValue)

let config = URLSessionConfiguration.default
let userPasswordString = NSString(format: "%@:%@", credentials.userName, credentials.password)
let userPasswordData = userPasswordString.data(using: String.Encoding.utf8.rawValue)
let base64EncodedCredential = userPasswordData!.base64EncodedString(options: NSData.Base64EncodingOptions.lineLength64Characters)
let authString = "Basic \(base64EncodedCredential)"
config.httpAdditionalHeaders = ["Authorization" : authString, "Content-Type" : "text/xml;charset=UTF-8"]
config.timeoutIntervalForRequest = 10.0

// create the user request
let urlString = NSString(format: "https://%@:%@/ws/", credentials.server, credentials.port)
let url = URL(string: urlString as String)
var request = URLRequest(url: url!)
request.httpMethod = method
request.httpBody = bodyData
request.setValue("Basic \(base64EncodedCredential)", forHTTPHeaderField: "Authorization")
let session = Foundation.URLSession(configuration: config, delegate: self, delegateQueue:OperationQueue.main)

_ = session.dataTask(with: request, completionHandler: { (data, response, error) in

let responseParser = XMLParser(data: data!)
let responseParserDelegate = XMLResponseParser(operation: operation)
responseParser.delegate = responseParserDelegate
responseParser.parse()

// DEBUGGING OPTIONS
//print(response)
//print(NSString(data: data!, encoding: NSUTF8StringEncoding))
DispatchQueue.main.async(execute: {
self.response = data!
})


}).resume()
}



func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
completionHandler(Foundation.URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))
}
}

我是异步编程方面的新手,需要一些帮助。提前致谢。

最佳答案

使用信号量,以便前一个任务完成之前不会开始一个任务。这是一个演示

// The semaphore value is like the size of a token pool. After you've taken
// all the tokens in the pool, you must wait until a task returns its token
// back to the pool. Here we only have 1 token (1 request at a time)
let semaphore = DispatchSemaphore(value: 1)

// task1 is a request that will take at least 5 seconds to respond
let task1 = URLSession.shared.dataTask(with: URL(string: "https://httpbin.org/delay/5")!) { data, response, error in
print("Task1 is done")
semaphore.signal() // release the token
}

// task2 is a faster request
let task2 = URLSession.shared.dataTask(with: URL(string: "https://httpbin.org")!) { data, response, error in
print("Task2 is done")
semaphore.signal() // release the token
}

// Never wait on your main queue, always do that in the background
DispatchQueue.global(qos: .background).async {
semaphore.wait() // take a token, wait if needed.
// There will never be a wait here, but included for consistency
print("starting task 1")
task1.resume()

semaphore.wait() // take a token, wait if needed
print("starting task 2")
task2.resume()
}

有了信号量,输出就是你所期望的:

starting task 1
Task1 is done
starting task 2
Task2 is done

取出 2 行 semaphore.wait() 可以看到这两个请求将如何同时发送:

starting task 1
starting task 2
Task2 is done
Task1 is done

关于Swift:减慢对 WebService 的多次调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42284950/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com