Like when dispatch_async ends in swift

0

I'm trying to get several files in Json format with library Alamofire , and then these json I have to save them in a database sqlite in String .

The problem is that I made the insertion in the database before obtaining the file Json .

How can I indicate that it inserts into the database only if the asynchronous functions have finished, and that it waits until it is finished?

Here is an example of my code:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {
    var menuJson = String()
    var comerciosJson = String()
    var coloresJson = String()
    var textosJson = String()

    dispatch_async(dispatch_get_main_queue()) {
        menuJson = self.getJson("http://url_al_archivo.json")
        print(menuJson)
    }
    dispatch_async(dispatch_get_main_queue()) {
        comerciosJson = self.getJson("http://url_al_archivo.json")
        print(comerciosJson)
    }
    dispatch_async(dispatch_get_main_queue()) {
        coloresJson = self.getJson("http://url_al_archivo.json")
        print(coloresJson)
    }
    dispatch_async(dispatch_get_main_queue()) {
        textosJson = self.getJson("http://url_al_archivo.json")
        print(textosJson)
    }
    dispatch_async(dispatch_get_main_queue()) {
        print("1")
        print(menuJson)
        print("2")
        print(comerciosJson)
        print("3")
        print(coloresJson)
        print("4")
        print(textosJson)
        GestorDB.addConfiguracion(menuJson, comerciosJson: comerciosJson, coloresJson: coloresJson, textosJson: textosJson)
        let valores = GestorDB.getConfiguracion()
        while valores.next() == true {
            let menuJson  = valores.stringForColumn("menu").stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
            let comerciosJson  = valores.stringForColumn("comercios").stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
            let coloresJson  = valores.stringForColumn("colores").stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
            let textosJson  = valores.stringForColumn("textos").stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())

            print("11")
            print(menuJson)
            print("22")
            print(comerciosJson)
            print("33")
            print(coloresJson)
            print("44")
            print(textosJson)

        }
    }
}

And this is the function with which I get the Json

func getJson(url: String) -> String {
    var responseString = String()
    Alamofire.request(.GET, url).responseJSON { response in
        if let valorJson = response.data {
            let json = NSString(data: valorJson, encoding: NSUTF8StringEncoding)
            responseString = json as! String
        }
    }
    return responseString
}
    
asked by 11.08.2016 в 13:40
source

1 answer

0

The problem is that you do not have anything clear how dispatch works. You can not have several dispatch_get_main_queue() , only one. A correct structure would be ...

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {

    // Código A
    // Código que es necesario ejecutar en otro hilo
    // normalmente en background

    dispatch_async(dispatch_get_main_queue()) {

        // Código B
        // Una vez acaba la ejecución del código A
        // se ejecuta este bloque

    }

}
    
answered by 11.08.2016 / 18:38
source