6

I am attempting to perform an alamofire post request in swift

func checkIfUserExistsInDB(userName: String) -> NSString
{

  print ("IN")
  var info: NSString = ""

  Alamofire.request(.POST, "http://blablabla.com/getuserdata", parameters: ["queryValue": userName,], encoding:.JSON).responseJSON { request, response, result in
    switch result {
    case .Success(let JSON):
        info = NSString(data: JSON.dataUsingEncoding(NSUTF8StringEncoding)!, encoding: NSUTF8StringEncoding)!

    case .Failure(let data, _):
        print ("IN")
        if let data = data {
            info = (NSString(data: data, encoding: NSUTF8StringEncoding)!)

            print (info)
        }

    }
  }

  return info
}

but I am running into trouble making it synchronously. I am aware that making an asynchronous function (like the one provided by Alamorfire) is not generally accepted but in my case I have to do it synchronously.

vadian
  • 274,689
  • 30
  • 353
  • 361
mark
  • 153
  • 1
  • 10
  • 1
    Try to understand the asynchronous pattern. There is always a way to conform to it. In your case implement a completion block to return the data, for example `func checkIfUserExistsInDB(userName: String, completion:(String) -> Void)` – vadian Apr 25 '16 at 16:00
  • could you elaborate? I am very new to swift – mark Apr 25 '16 at 16:09
  • I wrote an answer. – vadian Apr 25 '16 at 16:16
  • thanks man it works really well – mark Apr 25 '16 at 16:52
  • Reading comments and answers here I wonder: do you people test?! Integration-testing a sequence of asynchronous requests leads directly to callback hell. Also, when I use Alamofire in a (small) script I *have* to wait, otherwise the main thread (and therewith the program) will just terminate. It's not only apps, guys... – Raphael May 09 '17 at 12:39

1 Answers1

3

It's quite easy to implement a completion block in Swift.

This is your function with a completion block

func checkIfUserExistsInDB(userName: String, completion:(String) -> Void)
{
  Alamofire.request(.POST, "http://blablabla.com/getuserdata", parameters: ["queryValue": userName,], encoding:.JSON).responseJSON { request, response, result in
    switch result {
    case .Success(let JSON):
      let info = String(data: JSON.dataUsingEncoding(NSUTF8StringEncoding)!, encoding: NSUTF8StringEncoding)!
      completion(info)

    case .Failure(let data, _):
      if let errorData = data, info = String(data: errorData, encoding: NSUTF8StringEncoding) {
        completion(info)
      }
    }
  }
}

and can be called with (info is the asynchronously returned string)

checkIfUserExistsInDB("string") { (info) in
  print(info)
}
vadian
  • 274,689
  • 30
  • 353
  • 361
  • 4
    I don't understand the upvotes this question , how is it even relevant ? OP is asking about how to make async request sync , answer states how to use completion blocks , they are async in nature. – vishal dharankar May 29 '19 at 07:16
  • @vishaldharankar The message is: *Don't make an asynchronous task synchronous*. And the OP asked me explicitly to provide a solution with completion handler. – vadian May 29 '19 at 07:19
  • 2
    Still same , it’s not answer to original question OP should have asked it as a different question , quite misleading – vishal dharankar May 29 '19 at 07:20