我在Swift應用程序中有一個奇怪的行爲,我目前不明白。每次執行NSURLSessionTask需要比其他更長的時間
我已經子類NSOperation創建不同的操作,可以通過NSURLSession/NSURLSessionTask調用Rest-WebServices。這在一般情況下工作正常。
在我的情況下,我必須先後執行許多這些操作。比方說,我創建了一個包含30個NSOperations的「鏈」,並設置依賴關係來逐個執行它們。
現在我可以重現這樣一種行爲,即每執行一次這樣的操作,就會比其他操作花費更多的時間。看起來好像執行「睡眠」了將近10秒,然後繼續執行。我可以排除,具體的Web服務調用是問題。因爲如果我改變執行順序,它仍然是「掛起」的第11個操作。
當前我正在創建一個NSURLSession(defaultConfiguration)的新實例,在每個操作的執行過程中。昨天我試着創建一個NSURLSession的靜態實例,並在執行期間創建NSURLSessionTask的實例。而現在「衣架」已經消失了!不幸的是我不能這樣做,因爲對於某些操作NSURLSessionDelegate必須是不同的,但是這個委託必須在初始化期間傳遞。
有沒有人遇到類似的行爲?
首先,我認爲我的代碼太複雜,無法發佈。但在Ketans評論後,我會試一試。我已將它縮減爲最重要的部分。我希望這有助於展示我的問題。如果你需要更多的細節,請讓我知道。
class AbstractWebServiceOperation: NSOperation {
// VARIANT 2: Create a static NSURLSession only once --> The "sleep" DOES NOT occur!
static let SESSION = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
init(servicePath:String, httpMethod:String) {
// This is an 'abstract' class, that will be subclassed for concrete webService calls that differ in servicePath for URL, HTTP Method and parameters
}
// Override start() function of NSOperation to do webService call. NSOperations vars (ready, executing, finished) are overridden too, to get NSOperation "waiting" for the webService result. But I don't think it is relevant for the issue. So I did leave it out.
override func start() {
super.start()
// [...]
if let request = createRequest()
{
let task = createTask(request)
task.resume()
}
// [...]
}
// Creates the concrete NSURLRequest by using the service path and HTTP method defined by the concrete subclass.
private func createRequest()-> NSMutableURLRequest? {
// [...]
let webServiceURL = "https://\(self.servicePath)"
let url = NSURL(string: webServiceURL)
let request = NSMutableURLRequest(URL: url!)
request.timeoutInterval = 60
request.HTTPMethod = self.httpMethod
request.addValue("application/json;charset=UTF-8", forHTTPHeaderField: "Content-Type")
request.addValue("application/json;charset=UTF-8", forHTTPHeaderField: "Accept")
return request;
}
// Creates the concrete NSURLSessionTask for the given NSURLRequest (using a completionHandler defined by getCompletionHandler())
func createTask(request:NSURLRequest) -> NSURLSessionTask
{
// VARIANT 1: Create a new NSURLSession every time a AbstractWebServiceOperation is executed --> The "sleep" occurs!
let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration(), delegate: nil, delegateQueue: nil)
return session.dataTaskWithRequest(request, completionHandler:getCompletionHandler())
// VARIANT 2: Create a static NSURLSession only once --> The "sleep" DOES NOT occur!
return AbstractWebServiceOperation.SESSION.dataTaskWithRequest(request, completionHandler:getCompletionHandler())
}
// Returns the completion handler for the NSURLSessionTask (may be overriden in subclass)
func getCompletionHandler() -> (NSData?, NSURLResponse?, NSError?) -> Void
{
return completionHandler
}
// Default completion handler
lazy var completionHandler:(NSData?, NSURLResponse?, NSError?) -> Void = {(data : NSData?, response : NSURLResponse?, error : NSError?) in
// default completion handling
}
}
添加你的代碼,你如何做到這一點! – Lion