0

That is how my code looks right now. I am trying to launch a thread but my code goes to recursive cycle. I would like to get my error reason from you or get Swift analogue of wonderful Java Thread.join() function which is very useful in this situation.

var ret: JSON = JSON("{\"code\":\"-3\"}")
        var cont: Bool = true

    dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), {

        let req = Alamofire.request(.POST, "https://pornhub.com", parameters: parameters).validate().responseJSON { response in
            switch response.result {
            case .Success(let data):
                let json = JSON(data)
                print("TEST: " + ret.string!)
                ret = json
            case .Failure(let error):
                print("TEST: " + ret.string!)
                ret = (JSON("{\"code\":\"-2\"}"))
            }
        }

        dispatch_async(dispatch_get_main_queue(), { () -> Void in

            cont = false

        })

    })

    while(cont) {
        sleep(1)
    }

    return ret

2 Answers2

0

Here is how you should do it:

func post(parameters: [String : AnyObject], handler: Result<JSON, NSError> -> Void) {
    Alamofire.request(.POST, "https://hehe.com", parameters: parameters)
        .validate().responseJSON { response in
        switch response.result {
        case .Success(let jsonObject):
            let json = JSON(jsonObject)
            handler(.Success(json))
        case .Failure(let error):
            handler(.Failure(error))
        }
    }
}

You can call this method like this:

post(["some" : "parameters"]) { response in
    // The following part is called asynchronously, therefore you need to wrap UI updates in a dispatch main block:

    dispatch_async(dispatch_get_main_queue()) {
        switch response {
        case .Success(let json):
            print(json) // Update your UI with the json here
        case .Failure(let error):
            print(error) // Update your UI with an error here
        }
    }
}
Silvan Mosberger
  • 1,786
  • 14
  • 19
-1

Since you are blocking the main thread anyway why even use async in this case?

If you do want to not block the main thread (usually the UI thread and usually a very bad idea to do) you should call the method you provided in code and pass in a "completion" block. That completion block will have the ret value and will allow your process to continue. It may be a little difficult to see with that explanation, so I'll try to explain it with pseudo code:

function mainThread()
{
    var somethingINeed = firstAttempt()
    doSomethingWithReturn(somethingINeed);
}

function firstAttempt()
{
    var ret = doSomethingLong()
    return ret
}

So that just block the main thread and does your long task. Instead of that, and instead of your async with blocking, try something like this (again, pseudo code):

function mainThread()
{
    secondAttempt(method(var ret)
    {
        doSomethingWithReturn(ret);
    })
}

function secondAttempt(functionBlock)
{
    async{
        var ret = doSomethingLong()
        functionBlock(ret)
    }
}
Putz1103
  • 6,211
  • 1
  • 18
  • 25