简体   繁体   English

将 DispatchGroup 与流式 API 一起使用?

[英]Using DispatchGroup with Streaming API?

I'm currently using a DispatchGroup to be notified when two API calls have completed, and then combine both responses into 1 object, that I then return in a completion handler.我目前正在使用 DispatchGroup 在两个 API 调用完成时收到通知,然后将两个响应组合成 1 个 object,然后我在完成处理程序中返回。

This works for rest apis, however once I use this with two streaming calls, the app crashes because of the continuous firing / uneven dispatchGroup.leave count.这适用于 rest api,但是一旦我将它与两个流式调用一起使用,由于连续触发/不均匀的 dispatchGroup.leave 计数,应用程序会崩溃。

What is another way I can accomplish my goal, or is there something I can do to continue to use a DispatchGroup?还有什么方法可以实现我的目标,或者我可以做些什么来继续使用 DispatchGroup? Below is a quick example to showcase what I'm doing.下面是一个简单的例子来展示我在做什么。

func fetchPets(completion: @escaping (Result<[Pet], Error>) -> Void) {
    let dispatchGroup = DispatchGroup()
    let dogs: [Dog] = []
    let cats: [Cat] = []

    // Make streaming call 1
    dispatchGroup.enter()
    fetchDogs(completion: () -> Void) {
        // Do something (transform data) and add dogs to array
        dispatchGroup.leave()
    }

    // Make streaming call 2
    dispatchGroup.enter()
    fetchCats(completion: () -> Void) {
        // Do something (transform data) and add cats to array
        dispatchGroup.leave()
    }

    // Combine both responses once both calls complete
    dispatchGroup.notify(queue: .main) {
        // Do something with the stuff
        let pets = Pet(...)
        completion(pets)
    }
}

You can manually count if the two of them finished or not您可以手动计算他们两个是否完成

class YourClass {
    
    var totalRequest = -1
    var requestLoaded = -1
    
    
    func fetchPets() {
        fetchDogs()
        fetchCats()
    }
    
    func fetchDogs() {
        totalRequest += 1
        APICall.getDogsData { (response) in
            self.requestLoaded += 1
            // Do something (transform data) and add dogs to array
            self.checkIfAllRequestLoaded()
        }
    }
    
    func fetchCats() {
        totalRequest += 1
        APICall.getCatsData { (response) in
            self.requestLoaded += 1
            // Do something (transform data) and add cats to array
            self.checkIfAllRequestLoaded()
        }
    }
    
    // Combine both responses once both calls complete
    func checkIfAllRequestLoaded() {
        if requestLoaded == totalRequest {
            // Do something with the stuff
        }
    }
    
}

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

相关问题 使用Swift 3中的DispatchGroup()执行任务? - Using DispatchGroup() in Swift 3 to perform a task? 在基本计数器类上使用 DispatchGroup 的好处 - Benefit of using DispatchGroup over a basic counter class 使用 DispatchGroup 时如何超时? - How to have a timeout when using DispatchGroup? 在Swift中使用dispatchGroup处理一系列异步函数时出错 - Error in handling a series of async functions using dispatchGroup in Swift Swift 4 异步调用 for 循环使用 DispatchGroup、DispatchQueue 和 DispatchSemaphore 按顺序执行 - Swift 4 async call with for loop execute in order using DispatchGroup, DispatchQueue and DispatchSemaphore 使用 DispatchGroup 时异步调用阻塞主线程 - Async call blocking main thread when using DispatchGroup Swift 3 - 使用DispatchGroup()等待每个URL会话的完成? - Swift 3 - using DispatchGroup() to wait for completion for each URL session? 使用 DispatchQueue / DispatchGroup 调用两个函数 - calling two functions after each other using DispatchQueue / DispatchGroup 等待循环完成 DispatchGroup 使用 Swift 从 firebase 获取数据 - Waiting for loop to finish with DispatchGroup getting data from firebase using Swift 如何使用 DispatchGroup 将值设置为 while 循环中的条件? - How to set a value as a condition in while loop using DispatchGroup?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM