最佳方法在从多个API获取结果后执行某项操作 Swift

huangapple go评论74阅读模式
英文:

Best way to do something after getting result from multiple APIs swift

问题

我同时发送了两个异步调用,并希望在从两个API获得结果时执行另一个任务...

其中一个API实际用于上传视频,可能需要一些时间...即使应用程序从后台返回或者在用户终止应用程序后重新打开(如果上次的结果不成功,我会重新上传视频)...

我希望在两个API都成功获得结果时执行某些操作...

以前我使用了DispatchGroup(),但我发现它并不是一个非常有效的解决方案,因为如果在调用group.enter()后多次调用group.leave(),应用程序会崩溃。用户可以使用以下行来检查count,然后在离开组之前:

self.group.debugDescription.components(separatedBy: ",").filter({$0.contains("count")}).first!.components(separatedBy:CharacterSet.decimalDigits.inverted).filter({Int($0) != nil})

但对于这样一个小任务来说,这有点过于复杂,所以我正在尝试寻找更好的解决方案来处理这样的任务。

英文:

I am sending two async calls at the same time and want to perform another task when getting result from both APIs...

One of the APIs is actually used to upload the video which can take time ... the task may complete even after the app comes from the background or maybe reopen after being terminated by the user (I re-upload the video if the last result was not successful)...

I want to perform some action when both APIs got successful results...

Previously I was using DispatchGroup() but I didn't find it a much effective solution since the app got crashed if you call group.leave() one extra time after calling group.enter(). User can check for the count before leaving the group with following line:

self.group.debugDescription.components(separatedBy: ",").filter({$0.contains("count")}).first!.components(separatedBy:CharacterSet.decimalDigits.inverted).filter({Int($0) != nil}) 

but this is overkill for such a small task that's why I am trying to find some better solution for such tasks.

答案1

得分: 3

你可以使用DispatchGroup,同时检查这个链接,我希望它能解决你的问题。

https://stackoverflow.com/questions/50557431/how-to-do-two-concurrent-api-calls-in-swift-4

func downloadDetails(){
    let dispatchGroup = DispatchGroup()

    dispatchGroup.enter()   // <<---
    WebServiceManager.getAData(format:A, withCompletion: {(data: Any? , error: Error?) -> Void in

        if let success = data {

            DispatchQueue.main.async {
                (success code)
                dispatchGroup.leave()   // <<----
            }
        }
    })

    dispatchGroup.enter()   // <<---
    webServiceManager.getBData(format: B, withCompletion: {(data: Any? , error: Error?) -> Void in

        if let success = data {

            DispatchQueue.main.async {
               (success code)
               dispatchGroup.leave()   // <<----
            }
        }
    })

    dispatchGroup.notify(queue: .main) {
        // 在两者都完成时你想做的任何操作
    }
}
英文:

you can use DispatchGroup, also check this link i hope it will solve your problem.

https://stackoverflow.com/questions/50557431/how-to-do-two-concurrent-api-calls-in-swift-4

func downloadDetails(){
let dispatchGroup = DispatchGroup()

dispatchGroup.enter()   // <<---
WebServiceManager.getAData(format:A, withCompletion: {(data: Any? , error: Error?) -> Void in

    if let success = data {

        DispatchQueue.main.async {
            (success code)
            dispatchGroup.leave()   // <<----
        }
    }
})

dispatchGroup.enter()   // <<---
webServiceManager.getBData(format: B, withCompletion: {(data: Any? , error: Error?) -> Void in

    if let success = data {

        DispatchQueue.main.async {
           (success code)
           dispatchGroup.leave()   // <<----
        }
    }
})

dispatchGroup.notify(queue: .main) {
    // whatever you want to do when both are done
}

}

huangapple
  • 本文由 发表于 2023年7月3日 03:32:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76600489.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定