ios - Populate array of custom classes from two different network requests -


in swift ios project, trying populate array of custom class objects using json data retrieved alamofire , parsed swiftyjson. problem, though, combining results of 2 different network request and then populating uitableview resulting array.

my custom class implemented:

class teamitem: printable { var name: string? var number: string?  init(sqljson: json, nallenjson: json, numinjson: int) {     if let n = sqljson[numinjson, "team_num"].string! string! {         self.number = n     }     if let name = nallenjson["result",0,"team_name"].string! string! {         self.name = name     }  }  var description: string {     return "number: \(number) name: \(name)" } } 

here viewdidload():

    override func viewdidload() {     super.viewdidload()       tableview.datasource = self     refresh() {         () -> void in             self.tableview(self.tableview, numberofrowsinsection: self.teamsarr.count)             self.tableview.reloaddata()         item in self.teamsarr {             println(item)         }             return         }     self.tableview.reloaddata()     } 

which goes refresh() method:

func refresh(completionhandler: (() -> void)) {     populatearray(completionhandler) } 

and finally, populatearray():

    func populatearray(completionhandler: (() -> void)) {     sqlhelper.getdata("http://cnidarian1.net16.net/select_team.php", params: ["team_num":"all"]) {         (result: nsdata) in         let jsonobject : anyobject! = nsjsonserialization.jsonobjectwithdata(result, options: nsjsonreadingoptions.mutablecontainers, error: nil)         let json = json(jsonobject)         self.json1 = json         println(json.count)         var = 0; < json.count; ++i {             var teamnum = json[i,"team_num"].string!             nslog(teamnum)             alamofire.request(.get, "http://api.vex.us.nallen.me/get_teams", parameters: ["team": teamnum])                 .responsejson { (req, res, json, err) in                     let json = json(json!)                     self.json2 = json                         self.teamsarr.append(teamitem(sqljson: self.json1, nallenjson: self.json2, numinjson: i))             }         }         completionhandler()     } } 

the first problem had i in loop reached 3 , caused errors when thought shouldn't because json array contains 3 entries. other main problem table view empty until manually triggered reloaddata() reload button in ui, , there problems data in tables.

really appreciate assistance, new ios , swift , dealing alamofire's asynchronous calls confused me. code have been writing has grown large , generated many little errors, thought there better way of achieving goal. sorry long-winded question, , in advance responses!

the alamofire request returns , in parallel executes closure, take time complete. completion handler called right after alamofire returns, data aren't yet available. need call within alamofire closure - ensures called after data became available.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -