Why is my request blocking events?

1

I have implemented an http json request that works fine, but it prevents the user from clicking on it and can access the information only when it is downloaded.

Example: I have a UITableView with 3 Items, if I click on one of them and in that view is a request it will not enter the screen until it is ready.

Request class example:

class Requisicao {

    let urlDefault = "http://puc.vc/painel/webservice/"

    func getJSON(urlToRequest: String) -> NSData{
        let urlFull = urlDefault + urlToRequest
        return NSData(contentsOfURL: NSURL(string: urlFull)!)!
    }

    func parseJSON(inputData: NSData) -> NSDictionary{
        var error: NSError?
        var boardsDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(inputData, options: NSJSONReadingOptions.MutableContainers, error: &error) as! NSDictionary

        return boardsDictionary
    }
}

Call Sample:

class ProcedAcadViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let request = Requisicao()
        let data = request.getJSON("procedimentosacademicos/")
        let json = request.parseJSON(data)
        println(json)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

}

How can I solve this problem?

    
asked by anonymous 28.11.2015 / 21:35

2 answers

1

Gabriel, it was not very clear to me what the problem is exactly. From what I understand you want to make the request and once it finishes making a screen transition. There are several abstractions to work with in iOS competition, the most common being NSOperation and Grand Central Dispatch. Using GDC, a very recurrent flow is as follows:

let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) {
    // realiza alguma operação em background (ex: requisição ao web service)
    dispatch_async(dispatch_get_main_queue()) {
        // atualiza a tela após completar a operação
    }
}

dispatch_async receives two parameters, a queue and a block of code to execute on that queue. You can create your own queues or use those created by the system. In the example I fired a block of code to run in the background (without blocking the interface). As soon as this operation finishes I do some updating on the screen, which should always be done in the main thread dispatch_get_main_queue () )

    
28.11.2015 / 23:30
1

Use some asynchronous network library, type AlamoFire .

class ProcedAcadViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.makeRequest();
    }

    func makeRequest() {
        Alamofire.request(.GET, "http://puc.vc/painel/webservice/procedimentosacademicos/")
                 .responseJSON { response in
                      debugPrint(response)
                      // neste ponto vc tem um JSON e 
                      // podera atualizar o modelo e a table view
                 }
    }

}
    
02.12.2015 / 00:59