简体   繁体   中英

How to stop DispatchGroup after time? iOS

I'm working with DispatchGroup , I use this to execute two requests from two different servers. 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? Or how can I stop the 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()
            }
        }
    }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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