ios - Running a segue after selecting a table once a Asynchronous request is finished -


i'm trying run url request json file after table row selected, based on row unique id sent url request , different json generated. here prepareforsegue

    // mark: - navigation  // in storyboard-based application, want little preparation before navigation override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) {      // new view controller using segue.destinationviewcontroller.     var divisionscene = segue.destinationviewcontroller as! divisionviewcontroller      // pass selected object new view controller.     if let indexpath = tableview.indexpathforselectedrow() {     let arrayindex = indexpath.row        //println("index: \(arrayindex)")          torneoidtransfer = torneos[arrayindex].torneoid         //println("\(torneoidtransfer)")           //check second url second request type same token         //sets url string using token         let tercerurl = nsurl(string: "http://api.zione.mx/get.json.asp?tr=3&tkn=\(tkn)&tor=\(torneoidtransfer)")          //initializes request          let request = nsurlrequest(url: tercerurl!)         nsurlconnection.sendasynchronousrequest(request, queue: nsoperationqueue.currentqueue()) { response, jsondatarequest3, error in             let datarequest3 = jsondatarequest3              //takes data, saves json             let tercerjson = json(data: jsondatarequest3)              //checks see contents != nil, meaning json file found             if tercerjson != nil {                  //checks amount of tournaments available in order generate table.                 let divisioncount = tercerjson["lista-divisiones"].count                 //sets global variable numero de torneos                 numerodedivisiones = divisioncount                   //for loop go insert torneo nuevo each id , name using count above                 var index = 0; index < divisioncount; ++index {                     var divisionid = int(tercerjson["lista-divisiones" ][index]["divisionid"].number!)                     var nomdivision = tercerjson["lista-divisiones"][index]["nomdivision"].string                     //println("\(divisionid)")                     //println("\(nomdivision)")                     var divisionnuevo = listadivisiones(divisionid: divisionid, nomdivision: nomdivision!)                     divisiones.append(divisionnuevo)                     numerodedivisiones = 10                     print("who first")                 }             }             print("\(divisiones[0].nomdivision)")         }     } } 

and created segway dragging table cell new view controller. when click table cell transition occurs instantly, before request has chance finish , result no data displayed.

it ideal fetch , process data in background, long before user ever selects table row. if not possible, suggest having destination view controller url request. url request happens asynchronously, never have chance finish before source view controller deallocated.

in source view controller, modify prepareforsegue

override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) {     var divisionscene = segue.destinationviewcontroller as! divisionviewcontroller      if let indexpath = tableview.indexpathforselectedrow() {         let arrayindex = indexpath.row          torneoidtransfer = torneos[arrayindex].torneoid          let tercerurl = nsurl(string: "http://api.zione.mx/get.json.asp?tr=3&tkn=\(tkn)&tor=\(torneoidtransfer)")          divisionscene.fetchdataaturl(tercerurl)     } } 

you'll need define method inside destination view controller handle fetching.

func fetchdataaturl(url: nsurl) {     let request = nsurlrequest(url: tercerurl!)     nsurlconnection.sendasynchronousrequest( // ... fetching logic here } 

you'll need logic display data once arrives. suggest putting request's completion callback (or rather, having callback trigger display update). if divisionscene tableview, might able call reloaddata (after update data source). if not, you'll need other way update ui. if updating ui, make sure dispatch main queue part.

doing way @ least passes url loading responsibility destination view controller, @ least around when data gets there.


Comments