如何同步多个Alamofire请求

时间:2016-04-18 11:11:00

标签: ios swift alamofire

我有多个请求:

var data1: MyData?
var data2: MyData?
var data3: MyData?

func makeRequest(url: String, completion: (result: ResponseResult, data: MyData?) -> Void){
    Alamofire.request(.GET, url).responseJSON { response in
        switch response.result {
        case .Success(let JSON):
        completion(result: .Success, MyData(JSON))
        case. Failure(let error):
        completion(result: .Failure, nil)
        }
    }
}

makeRequest(url1){ result, data in
    data1 = data
}
makeRequest(url2){ result, data in
    data2 = data
}
makeRequest(url3){ result, data in
    data3 = data
}

收到所有数据后,我必须调用以下函数:

workWithData(data1, data2: data2, data3: data3)

如何在这种情况下为这三个请求设置障碍?

3 个答案:

答案 0 :(得分:7)

你必须使用DispatchGroup,不要忘记死锁。

var data1: MyData?
var data2: MyData?
var data3: MyData?

func makeRequest(url: String, completion: (result: ResponseResult, data: MyData?) -> Void){
    Alamofire.request(.GET, url).responseJSON { response in
        switch response.result {
        case .Success(let JSON):
            completion(result: .Success, MyData(JSON))
        case. Failure(let error):
            completion(result: .Failure, nil)
        }
    }
}

let downloadGroup = DispatchGroup()

downloadGroup.enter()
downloadGroup.enter()
downloadGroup.enter()

makeRequest(url1){ result, data in
    data1 = data
    downloadGroup.leave()
}
makeRequest(url2){ result, data in
    data2 = data
    downloadGroup.leave()
}
makeRequest(url3){ result, data in
    data3 = data
    downloadGroup.leave()
}

DispatchQueue.global(qos: .background).async {
    downloadGroup.wait()
    DispatchQueue.main.async {
        workWithData(data1, data2: data2, data3: data3)
    }
}

答案 1 :(得分:1)

信号量应该适合你。考虑一下:

var data1: NSData?
var data2: NSData?
var data3: NSData?

func makeRequest(url: String, completion: (data: NSData?) -> Void){

    let request = Alamofire.request(.GET, "https://google.com").responseJSON(queue: dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { closureResponse in

        completion(data: NSData())
    }
}

let sem = dispatch_semaphore_create(0)

makeRequest("1"){ data in
    data1 = data
    dispatch_semaphore_signal(sem)
}
makeRequest("2"){ data in
    data2 = data
    dispatch_semaphore_signal(sem)
}
makeRequest("2"){ data in
    data3 = data
    dispatch_semaphore_signal(sem)
}

dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER)
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER)
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER)

print("123")

根据@James评论,我花了一些时间和Alamofire一起玩。我想到的是默认情况下它会在主队列上提供回调。从我的角度来看,这是不好的,我倾向于最小化主线程负载。我建议使用并发队列进行回调传递。

答案 2 :(得分:0)

我认为您应该检查是否在Alamofire.request结束时继续:

func makeRequest(url: String, completion: (result: ResponseResult, data: MyData?) -> Void){
    Alamofire.request(.GET, url).responseJSON { response in
        switch response.result {
        case .Success(let JSON):
            completion(result: .Success, MyData(JSON))
        case. Failure(let error):
            completion(result: .Failure, nil)
        }

        if data1 != nil && data2 != nil && data3 != nil {
            workWithData(data1, data2: data2, data3: data3)
        }
    }
}