0
我正在處理向web服務發送多個請求的應用程序。隨着我對開發的進一步深入,我發現web服務越來越不堪重負,我需要稍微放慢一點。我想單獨發送每個請求到Web服務,並等待前一個請求完成後再發送下一個請求。下面是使用一個循環,使對web服務的調用函數:Swift:緩慢多次調用WebService
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!))
}
}
我是個新手,當談到Asyncronous編程和可以使用一些幫助。提前致謝。
我會看看我是否可以完成這項工作,但是每個任務都沒有單獨編碼,所以我不確定這是否可行。正如您在我的示例中所看到的,該任務使用數組中的操作列表中的循環運行多次。 – GED125
對不起,我做了這個工作!非常感謝你的建議。這一個讓我長時間封鎖了!我在全球範圍內定義了信號量,解決了循環中的問題。非常感謝! – GED125