- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 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/
我有一个可以从网站下载文件的 iOS 应用程序。我创建了一个 NSURLSession在一个类(class)Downloads来管理它们。 Downloads类有 NSMutableArray使用我的
您好,我正在尝试从 swift 客户端在 Java 服务器上执行简单的身份验证。 java 服务器是一个 HTTP 服务器,其代码接受带有用户名和密码的“POST”请求。如果用户名和密码正确,服务器返
我好像遇到了麻烦。出于某种原因,如果我尝试检查我的 NSURLSessionTask 对象的类类型,它根本不起作用。如果我检查它们的 taskDescription 属性,如果我在启动任务之前设置它们
我想知道是否可以重新运行失败的 NSURLSessionDataTask。这是我遇到此问题的背景。 我有一个 Web 服务对象,它使用 AFNetworking 2.0 来处理请求。在我的一种方法中,
这是 Apple 文档中关于 NSURLSessionTask 类的 suspend 方法的内容 A task, while suspended, produces no network traffi
我正在使用带有后台 session 的 NSURLSessionDownloadTask 来实现我所有的 REST 请求。这样我就可以使用相同的代码,而不必考虑我的应用程序是在后台还是在前台。 我的后
我想创建文件下载管理器来下载多个具有下载百分比和播放暂停删除功能的文件。 我尝试使用以下代码成功下载多个文件...但无法添加进度条请帮忙 for (int i = 0; i 然后您需要遵循以下代码:
假设我有一个正在运行的NSURLSessionUploadTask。如果应用程序在该时间范围内被终止或崩溃,待处理的请求会发生什么?任务仍然会完成还是也会终止? 最佳答案 这是凭内存而来的,所以对它持
我几乎可以肯定,我最近看到了一种与网络相关的方法,该方法允许使用 NSDictionary 设置 URL 参数。 它是新 NSURLSession 类的一部分,但我找不到它。 本质上,而不是像...那
我使用 NSURLSessionTask 并且我正在尝试监控我的一些 HTTP 请求需要多长时间,当 NSURLSessionTask 实际上发出初始请求?如果这是 NSOperation 中的 NS
这个问题在这里已经有了答案: block until reverseGeocode has returned (1 个回答) 关闭 7 年前。 我有一个简单的 NSURLConnection 任务
我正在使用 AFNetwork 3.0 进行 HTTP 获取请求。我什至得到回应。但我想将响应数据作为 NSString。有人可以帮我如何获取响应数据作为 NSString 这是我的代码, AFHTT
我经常有这样的代码,当我有一个处理与 REST API 通信的类时,里面有很多方法使用 NSURLSession 任务,闭包作为回调。我还提供了其中一些方法的回调闭包,这样我就可以一个接一个地链接不同
制作 NSURLSessionTasks 串行队列的最佳实践是什么? 就我而言,我需要: 获取 JSON 文件中的 URL(NSURLSessionDataTask) 在该 URL 下载文件 (NSU
在我的 iOS 应用程序中,我使用 NSURLSessionTask 将 json 数据下载到我的应用程序。我发现当我直接从浏览器调用 url 时,我会得到一个最新的 json,而当它从应用程序中调用
我的 Swift 应用程序有一个奇怪的行为,我目前不明白。 我对 NSOperation 进行了子类化,以创建可以通过 NSURLSession/NSURLSessionTask 调用 Rest-We
我为我的应用程序编写了一个单独的 WebServices 类 - WebServices.m。 其中,我有几个静态方法,例如 setUserInput()、getUserProfile()、regis
当我关闭 WiFi 连接并在 iPhone 6s 10.2 模拟器上运行以下代码时,回调永远不会执行。我预计回调会很快触发,并出现“无互联网连接”之类的错误。 NSLog(@"request-star
我正在实现自定义 NSURLProtocol,并且在内部希望使用 NSURLSession 处理内部网络的数据任务,而不是 NSURLConnection。 我遇到了一个有趣的问题,想知道 NSURL
如果我创建一个 NSURLSessionDownloadTask,然后在它完成之前取消它,完成 block 似乎仍然会触发。 let downloadTask = session.downloadTa
我是一名优秀的程序员,十分优秀!