Why is my request blocking events?

Asked

Viewed 78 times

1

I implemented a json http request that works well, but it prevents the user from clicking by being able to access the information only when it is downloaded.

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

Example of request class:

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

Example of the Call:

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 ?

2 answers

1


Gabriel, it’s not very clear to me exactly what the problem is. From what I understand you want to make the request and as soon as it is finished make a screen transition. There are several abstractions to work with competition on iOS, the most common being Nsoperation and Grand Central Dispatch. Using GDC, a well recurring stream 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 be executed in this queue. You can create your own queues or use the ones created by the system. In the example I triggered a block of code to run in the background (without blocking the interface). As soon as this operation ends I do some updating on the screen, which should always be done in the main thread (dispatch_get_main_queue())

  • Take a look at the comment on the right answer http://stackoverflow.com/questions/24065536/downloading-and-parsing-json-in-swift

  • You asked why the request blocks events. This is because you perform the json download and Parsing operation on the main thread, which is responsible for handling events and rendering the UI. It is in this thread that the Uiviewcontroller lifecycle methods are called, such as viewDidLoad. To avoid this locking it is necessary to perform these operations outside the main thread.

  • This dispatch_async must be executed in func viewDidLoad?

  • It can be. There are actually several ways to do it. The important thing is not to do this kind of operation in the main thread. Otherwise it can crash the app and generate a bad user experience. Also, if the app depends on the request to continue browsing, it’s always good to show that it is processing something, for example using Mbprogresshud.

1

Use some asynchronous network library, like Alembic.

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

}
  • +1,In fact, at first I decided to create the functions that perform requests for learning, but since I have little time and much to do to go to a library that facilitates this is much better, was using Afnetworking but will migrate to Alamofire for being created in Swift.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.