Asynchronous / Synchronous Class Methods Swift Alamofire SwiftyJSON

So, I have the following:

class ViewController: UIViewController { class Identity{ let baseUrl = "superSecretURL" var _username: String = "" var _password: String = "" var _apiKey: String = "" init(){ } init(username: String, apiKey: String){ _username = username _apiKey = apiKey } init(username: String, password: String){ _username = username _password = password } func loginPassword() -> String{ var loginJSON = ["auth": ["passwordCredentials": ["username": _username, "password": _password]]]; var returnJSON: String request(.POST, baseUrl, parameters: loginJSON, encoding: .JSON) .responseJSON { (request, response, data, error) in if let anError = error { // got an error in getting the data, need to handle it println("error calling POST on /posts") println(error) } else if let data: AnyObject = data { // handle the results as JSON, without a bunch of nested if loops let post = JSON(data) // to make sure it posted, print the results println("JSON Returned") } } } } var i = Identity(username: "secretName", password: "complicatedPassword") override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. println("Before Call") println("After Call") } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 

Basically, I would like to be able to call println ("Before the call"), then get a response from the loginPassword () method, and then println ("After the call"). This, I believe, is synchronous, but I can’t figure out how to make it work, and the whole thread of things puzzled my head.

I basically want to say:

 if i.loginPassword(){ // do some login stuff }else{ // do some error stuff } 

Any help or pointers appreciated.

+6
source share
1 answer

You need to set a callback function that will be called whenever you want in your loginPassword() function.

This may be a way to achieve it:

 func loginPassword(callback: ((isOk: Bool)->Void)?) -> String{ var loginJSON = ["auth": ["passwordCredentials": ["username": _username, "password": _password]]]; var returnJSON: String request(.POST, baseUrl, parameters: loginJSON, encoding: .JSON) .responseJSON { (request, response, data, error) in if let anError = error{ // got an error in getting the data, need to handle it println("error calling POST on /posts") println(error) callback?(isOk: false) } else if let data: AnyObject = data{ // handle the results as JSON, without a bunch of nested if loops let post = JSON(data) // to make sure it posted, print the results println("JSON Returned") callback?(isOk: true) } } } 

and then...

 override func viewDidLoad() { super.viewDidLoad() var identity = Identity(username: "John Apleseed", apiKey: "213123123") identity.loginPassword { (isOK) -> Void in if (isOK) { //do good stuff here }else{ // do error handling here } } } 

UPDATE

Additionally, your calling function might look like this:

 override func viewDidLoad() { super.viewDidLoad() var identity = Identity(username: "John Apleseed", apiKey: "213123123") identity.loginPassword(handlePasswordRequest) } 

and you can add as many callback handlers as you might need without interfering with a bunch of nested closures ...

 private func handlePasswordRequest(isOK: Bool){ if (isOK) { //do good stuff here }else{ // do error handling here } } 

UPDATE 2

If you need to call a callback deep into the call hierarchy, you need to pass a callback as a parameter to all previous closures.

UPDATE 3

I would try RxAlamofire and all about RxSwift

+8
source

Source: https://habr.com/ru/post/987078/


All Articles