ios - How to check Alamofire request has completed -
i developing ipad application using swift. http requests use alamofire library. far have managed pull data api. problem since asynchronous call don't know how check whether request has completed. appreciated.
this code have implemented far
client class
func getusers(completionhandler: ([user]?, nserror?) -> ()) -> (){ var users: [user] = [] let parameters = [ "id": "123", "apikey": "1234", ] alamofire.request(.get, "api_url", parameters: parameters).responsejson() { (_, _, json, error) in let items = (json!.valueforkey("users") as! [nsdictionary]) item in items { var user: user = user() user.userid = item.valueforkey("id")! as? string user.username = item.valueforkey("name")! as? string user.group = item.valueforkey("group")! as? string users.append(user) } completionhandler(users, error) } }
main class
func getusers(){ facilityclient().getusers() { (users, error) -> void in if users != nil { self.users = users! } else{ println("error - \(error)") } } tableusers.reloaddata() }
thank you.
the closure part of alamofire request called, when request has completed. i've used code , commented line request hast finished:
alamofire.request(.get, "api_url", parameters: parameters).responsejson() { (_, _, json, error) in // // request has finished here. check if error != nil before doing else here. // let items = (json!.valueforkey("users") as! [nsdictionary]) item in items { var user: user = user() user.userid = item.valueforkey("id")! as? string user.username = item.valueforkey("name")! as? string user.group = item.valueforkey("group")! as? string users.append(user) } // // after creating user array call completionhandler, reports viewcontroller instance // completionhandler(users, error) }
Comments
Post a Comment