ios – How to stop ongoing dispatch group execution and start new one?


So I have a paginated API that I have to call every time a user enters a screen. The below code works fine but has one issue if say API calls are in progress and the user backs down and comes again then rather both execution starts simultaneously.
and has to wait for both executions to be completed.
So, What I want is to stop ongoing API calls and start new ones when requested new one
If this setup is wrong in any way please notify me as well what am I doing wrong?

func storeAllData(matchID: Int64) {
        GlobalVariable.isFetchingData = true
        dbManager = DbManager.shared
        if let lastScore = dbManager.getLastBallForCurrentMatch(matchID: matchID) {
            self.fetchAllScore(scoreID: lastScore.id ?? 0, matchID: matchID)
        } else {
            self.fetchAllScore(scoreID: 0, matchID: matchID)
        }
    }
    
    func fetchAllScore(scoreID: Int = 0, matchID: Int64) {
        let id = matchID
        backgroundQueue.async { [self] in
            autoreleasepool {
                fetchPaginatedData(matchID:Int(id), scoreID: scoreID, page: 1)
                dispatchGroup.wait()
                print("All API calls finished")
            }
        }
    }
    
    func fetchPaginatedData(matchID: Int,scoreID: Int = 0, page: Int) {
        dispatchGroup.enter()
        MatchesNetworkManager.sharedInstance.getAllScore(matchID: Int64(matchID), scoreID: Int64(scoreID), page: Int64(page), completionHandler: { [weak self] (allSocre, isSuccess, errorStr) in
            
            if isSuccess {
                if allSocre?.match?.scores != nil {
                    self?.oldScores.append(contentsOf: ((allSocre?.match?.scores)!))
                    self?.backgroundQueue.async {
                        if let scores = allSocre?.match?.scores as? [Scores] {
                            self?.dbManager.insertRecords(matchID: matchID, records: scores)
                            DispatchQueue.main.async {
                                self?.oldScores = []
                                if allSocre?.page_num ?? 0 == 1 {
                                    self?.updateScoreView()
                                }
                                if (allSocre?.page_num ?? 0) < (allSocre?.total_pages ?? 0) {
                                    self?.fetchPaginatedData(matchID: matchID,page: (allSocre?.page_num ?? 1) + 1)
                                } else {
                                    // All pages have been fetched
                                    GlobalVariable.isFetchingData = false
                                    print("All API calls finished")
                                    NotificationCenter.default.post(name: .refreshMarketView, object: nil, userInfo: nil)
                                    self?.dispatchGroup.leave()
                                }
                            }
                        }
                    }
                } else {
                    self?.updateScoreView()
                    GlobalVariable.isFetchingData = false
                }
                
            } else {
                print("API call error: \(errorStr)")
                self?.updateScoreView()
                GlobalVariable.isFetchingData = false
                self?.dispatchGroup.leave()
            }
        })
    }

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img