简体   繁体   English

如何在一段时间后停止DispatchGroup? iOS版

[英]How to stop DispatchGroup after time? iOS

I'm working with DispatchGroup , I use this to execute two requests from two different servers. 我正在使用DispatchGroup ,我使用它来执行来自两个不同服务器的两个请求。 When the response comes from two servers, DispatchGroup is called notify() , the problem is that one response from the server may not come (it happened once) how can DispatchGroup notify itself after some time? 当响应来自两个服务器时, DispatchGroup被称为notify() ,问题是来自服务器的一个响应可能不会(它发生一次) DispatchGroup如何在一段时间后通知自己? Or how can I stop the DispatchGroup ? 或者我如何停止DispatchGroup I made an example, but I think this is not what I need. 我做了一个例子,但我认为这不是我需要的。

Timer.scheduledTimer(withTimeInterval: 7.5, repeats: false) { [weak self] (timer) in
        guard let _self = self else { return }
        DispatchQueue.main.async {
            if _self.leaveDispatchGroupDigit == 0 {
                _self.requestGroup.leave()
                _self.requestGroup.leave()
            }
            if _self.leaveDispatchGroupDigit == 1 {
                _self.requestGroup.leave()
            }
        }
    }

在这种情况下,为请求创建超时是最干净的解决方案:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM