Async await swift ui

Viewed 47

I have the following code, I would like to use async-await for the two calls.

At the moment inside the function I used a kind of check variable, which when both are set the code is executed.

How can I do?

....
.onAppear {
    DispatchQueue.global(qos: .background).async {
        getRemoteHead(url: repoUrlStr)
        getRemoteBranch(url: repoUrlStr)
    }
}
    func getRemoteHead(url: String) {
        do {
            let branch = try Remote().getRemoteHEAD(url: url)
            if branch[0].contains("fatal:") {
                Log.warning("Error: getRemoteHead")
                activeSheet = .error("Error: getRemoteHead")
            } else {
                self.mainBranch = branch[0]
                self.selectedBranch = branch[0]
                self.check += 1
                if check == 2 {
                    check = 0
                    activeSheet = .select
                }
            }
        } catch {
            Log.error("Failed to find main branch name.")
        }
    }

    func getRemoteBranch(url: String) {
        do {
            let branches = try Remote().getRemoteBranch(url: url)
            if branches[0].contains("fatal:") {
                Log.warning("Error: getRemoteBranch")
                activeSheet = .error("Error: getRemoteBranch")
            } else {
                self.arrayBranch = branches
                self.check += 1
                if check == 2 {
                    check = 0
                    activeSheet = .select
                    DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
                        // Force a UI Update.
                        allBranches.toggle()
                    }
                }
            }
        } catch {
            Log.error("Failed to find branches.")
        }
    }
0 Answers
Related