2016-09-20 6 views
2

신속한 2.2에서 동시 API 호출을위한 코드를 작성했습니다. 신속한 2.2에서 신속한 3으로 변경하면 신속한 구문으로 문제가 발생합니다.스위프트 3 변환

let endPoints = [.email, .others] 
    let fetchGroup = dispatch_group_create() 
    let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
    dispatch_apply(endPoints.count, queue) { (index) in 
     let enumType = endPoints[index] 
     switch enumType { 
     case .email: 
      //Make email api call 
      break 
     case .others: 
      //Make other api 
      break 
     default: 
      break 
     } 
    } 

    dispatch_group_notify(fetchGroup, dispatch_get_main_queue()) { 
     if endPoints.count > 0 { 
      fail("error") 
     } 
    } 
+0

어떤 라인을 참조? –

+0

@ Mr.UB 특히, dispatch_apply 및 dispatch_group_notify – venky

+0

당신은 발송 그룹을 사용하는 것 같지 않습니다. 그룹에 알리는 것은별로 의미가 없습니다. 일반적으로 일련의 비동기 호출을 수행 할 때는 그룹을 사용하지만 dispatch_apply는 동기식이므로 일반적으로 비동기식 호출 루틴 호출시이를 사용하지 않습니다. – Rob

답변

3

는 여전히 dispatch_apply()을 기억하십니까 밖으로 도와주세요. 아직 거기에있어 새로운 이름이 있습니다. 당신이 전화를해야 지금부터 concurrentPerform() 자세한 내용은

let endPoints = [.email, .others] 
    let fetchGroup = DispatchGroup() 
    let queue = DispatchQueue.global (qos : .default) 
    DispatchQueue.concurrentPerform(iterations: endPoints.count) 
    { (index) in 
     let enumType = endPoints[index] 
     switch enumType { 
     case .email: 
      //Make email api call 
      break 
     case .others: 
      //Make other api 
      break 
     default: 
      break 
     } 
    } 
    DispatchGroup().notify(queue: DispatchQueue.main) { 
     if endPoints.count > 0 { 
      fail("error") 
     } 
    } 

특히 코드의 this