I have three function inside the repository class . I am calling the function in order first createUser, then submitApplication and finally submitMemorableWord function . Condition is if the createUser function execution is successfully then it will go to submitApplication function , then if submitApplication function execution is successfully then flow will go to submitMemorableWord function.
I have set boolean flag into function execution block if the boolean flag return true then go to next code execution and call the appropriate function. I have debug it it and find out only createUser function is called.
I made boolean tag (success) to true on execute function and then I am expecting it should go to submit function and call submitApplication. I also set another boolean flag submitComplete into submit function and change the value to true when submit function is return successfully and finally I am checking if boolean tag submitComplete return true , that means submit function code execution is successfull then call complete function and respectfully return the result of the complete function.
of the never call submit or submitApplication function which is next function to be called..
Here is the code for Repository class....
import Foundation
class Repository {
init() {}
func createUser() async throws -> String {
"User"
}
func submitApplication() async throws -> Bool {
true
}
func submitMemorableWord() async throws -> Bool {
true
}
}
Here is the code for
import Foundation UseCase class
class UseCase {
let repository = Repository()
var success: Bool = false
var sumbitComplete: Bool = false
func execute() async throws {
do {
success = true
let createUser = try await repository.createUser()
} catch {
success = false
print("error " + error.localizedDescription)
}
func submit() async -> Bool {
if success {
do {
let submit = try await repository.submitApplication()
sumbitComplete = true
} catch {
return false
}
}
return true
}
}
func complete() async {
if sumbitComplete {
do {
let completeRequest = try await repository.submitMemorableWord()
} catch {
print("Sorry sometings went wrong..")
}
}
}
}
Here is code in view controller ..
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
Task { await FinalResult() }
}
func FinalResult() async {
let usecase = SimpleUseCase()
do {
let result = try await usecase.execute()
} catch {
print(error.localizedDescription)
}
}
}
didSetonexecute()and call thensubmit()if needed. But it might be simpler inexecuteto callsubmit()if success, etc.submitorcomplete. Alsosubmitis a nested function insideexecute. Is that really what you intended? Why don't you simply callsubmit()andcomplete()in order inside ` execute()`?