Ios 如何使用Alamofire创建一个好的模式来控制多个请求的依赖关系?例如request2需要request1的响应

Ios 如何使用Alamofire创建一个好的模式来控制多个请求的依赖关系?例如request2需要request1的响应,ios,swift,request,dependencies,alamofire,Ios,Swift,Request,Dependencies,Alamofire,我使用Alamofire同时发送多个请求。但是请求A需要请求B的名为token的响应。我会举一个例子: var token = "111111111111" let URLString1 = "http://httpbin.org/get?token=\(token)" Alamofire.request(.GET, URLString1, parameters: nil, encoding:.JSON).responseJSON { response in switch respo

我使用Alamofire同时发送多个请求。但是请求A需要请求B的名为token的响应。我会举一个例子:

var token = "111111111111"

let URLString1 = "http://httpbin.org/get?token=\(token)"
Alamofire.request(.GET, URLString1, parameters:  nil, encoding:.JSON).responseJSON { response in
    switch response.result {
    case .Success(let JSON):
        print("http1: \(JSON)")
        token = "22222222222"
    case .Failure(let error):
        print("Request failed with error: \(error)")
    }
}

//I want to wait for request1's response and get the new token value.
let URLString2 = "http://httpbin.org/get?token=\(token)"
Alamofire.request(.GET, URLString2, parameters:  nil, encoding:.JSON).responseJSON { response in
    switch response.result {
    case .Success(let JSON):
        print("http2: \(JSON)")
        print("token: \(token)")
    case .Failure(let error):
        print("Request failed with error: \(error)")
    }
}
我不想让request2影响request1的成功。 现在我的解决方案是:我给一个名为tokenChange的变量,我有一个数组来存储所有需要该令牌的请求。当令牌被更改时,我会从数组中逐个发送它们。但这个解决方案并不优雅。 那么有没有一个好的模式来控制这个问题呢?

我发现这个方法分派信号量。它可以解决我的问题。还有别的办法吗

var token = "111111"

let group = dispatch_group_create();

dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) { () -> Void in
    let semaphore = dispatch_semaphore_create(0)
    //token is 111111
    let URLString = "http://httpbin.org/get?token=\(token)"
    Alamofire.request(.GET, URLString, parameters:  nil, encoding:.JSON).responseJSON { response in
        switch response.result {
        case .Success(let JSON):
            print("http1: \(JSON)--class:")
            token = "222222"
            print("\n")
            dispatch_semaphore_signal(semaphore)
        case .Failure(let error):
            print("Request failed with error: \(error)")
        }
    }
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)

    //token is 222222
    let URLString2 = "http://httpbin.org/get?token=\(token)"
    Alamofire.request(.GET, URLString2, parameters:  nil, encoding:.JSON).responseJSON { response in
        switch response.result {
        case .Success(let JSON):
            print("http2: \(JSON)--class:")
            print("\n")
            print ("token is:\(token)")
        case .Failure(let error):
            print("Request failed with error: \(error)")
        }
    }

    //token is 2222222
    let URLString3 = "http://httpbin.org/get?token=\(token)"
    Alamofire.request(.GET, URLString3, parameters:  nil, encoding:.JSON).responseJSON { response in
        switch response.result {
        case .Success(let JSON):
            //                    token = (JSON["args"]!!["value"]) as! String
            print("http3: \(JSON)--class:")
            print("\n")
            print ("token is:\(token)")
        case .Failure(let error):
            print("Request failed with error: \(error)")
        }
    }
}

dispatch_group_notify(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) { () -> Void in
    NSLog("finish");
}

您需要在第一个请求中执行第二个url字符串,因为响应将出现在后台线程中,因此您的代码如下所示:

 var token = "111111111111"
    let URLString1 = "http://httpbin.org/get?token=\(token)"
    Alamofire.request(.GET, URLString1, parameters:  nil, encoding:.JSON).responseJSON { response in
        switch response.result {
        case .Success(let JSON):
            print("http1: \(JSON)")
            token = "22222222222"
        case .Failure(let error):
            print("Request failed with error: \(error)")
        }


    let URLString2 = "http://httpbin.org/get?token=\(token)"
    Alamofire.request(.GET, URLString2, parameters:  nil, encoding:.JSON).responseJSON { response in
        switch response.result {
        case .Success(let JSON):
            print("http2: \(JSON)")
            print("token: \(token)")
        case .Failure(let error):
            print("Request failed with error: \(error)")
        }
    }

    }

如果我同时发送10个请求。其他9个请求都需要第一个请求的响应。这个解决方案不可能是优雅的。有什么想法吗?所以我建议将boolean var responsed设置为false,并将其他请求包括在内,while responsed变量将为true,当第一个请求响应时,将boolean变量responsed更改为true。我希望这会有帮助是的,这个解决方案可以解决这个问题。但我认为它不是很优雅。除息的