Pesquisa Longa HTTP em Swift
Estou tentando implementar uma solução de pesquisa longa no Swift usando o iOS 8 ou superior.
Embora a solução, sem dúvida, funcione e deixe o thread principal livre para interações da interface do usuário, o uso da memória aumenta continuamente, por isso estou obviamente fazendo algo errado. A classe que escrevi é a seguinte:
enum LongPollError:ErrorType{
case IncorrectlyFormattedUrl
case HttpError
}
public class LongPollingRequest: NSObject {
var GlobalUserInitiatedQueue: dispatch_queue_t {
return dispatch_get_global_queue(Int(QOS_CLASS_USER_INITIATED.rawValue), 0)
}
var GlobalBackgroundQueue: dispatch_queue_t {
return dispatch_get_global_queue(Int(QOS_CLASS_BACKGROUND.rawValue), 0)
}
var longPollDelegate: LongPollingDelegate
var request: NSURLRequest?
init(delegate:LongPollingDelegate){
longPollDelegate = delegate
}
public func poll(endpointUrl:String) throws -> Void{
let url = NSURL(string: endpointUrl)
if(url == nil){
throw LongPollError.IncorrectlyFormattedUrl
}
request = NSURLRequest(URL: url!)
poll()
}
private func poll(){
dispatch_async(GlobalBackgroundQueue) {
self.longPoll()
}
}
private func longPoll() -> Void{
autoreleasepool{
do{
let urlSession = NSURLSession.sharedSession()
let dataTask = urlSession.dataTaskWithRequest(self.request!, completionHandler: {
(data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
if( error == nil ) {
self.longPollDelegate.dataReceived(data)
self.poll()
} else {
self.longPollDelegate.errorReceived()
}
})
dataTask.resume()
}
}
}
}
Eu tentei criar um perfil do aplicativo na Instruments, mas os resultados são confusos. Espero que alguém possa me apontar na direção certa.
obrigado