gpt4 book ai didi

ios - NSURLSessionTask 每执行 11 次所花费的时间就比其他执行时间长得多

转载 作者:行者123 更新时间:2023-11-30 13:07:54 25 4
gpt4 key购买 nike

我的 Swift 应用程序有一个奇怪的行为,我目前不明白。

我对 NSOperation 进行了子类化,以创建可以通过 NSURLSession/NSURLSessionTask 调用 Rest-WebServices 的不同操作。一般来说,这工作得很好。

就我而言,我必须连续执行许多这样的操作。假设我创建了一个由 30 个 NSOperations 组成的“链”,并设置了依赖项来逐一执行它们。

现在我可以重现这种行为,即每执行 11 次(?!)执行一次此类操作,就会比其他操作花费更长的时间。执行过程似乎“休眠”了近 10 秒才继续执行。我可以排除具体的 Web 服务调用是问题所在。因为如果我改变执行顺序,“挂起”的仍然是第 11 个操作。

目前,我正在执行每个操作期间创建 NSURLSession (defaultConfiguration) 的新实例。昨天我尝试创建 NSURLSession 的静态实例并仅在执行期间创建 NSURLSessionTask 的实例。而现在“衣架”不见了!不幸的是我不能这样做,因为 NSURLSessionDelegate 对于某些操作必须是不同的,但这个委托(delegate)必须在初始化期间传递。

有人经历过类似的行为吗?

首先我认为我的代码太复杂而无法发布。但在 Ketan 发表评论后,我会尝试一下。我已将其精简为最重要的部分。我希望这有助于表明我的问题。如果您需要更多详细信息,请告诉我。

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
}
}

最佳答案

Awww...我只是忘记在 webService 调用完成后调用 session.finishTasksAndInvalidate() 来使 session 无效。

这解决了我的问题!

关于ios - NSURLSessionTask 每执行 11 次所花费的时间就比其他执行时间长得多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39114554/

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