skip to Main Content

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)
    }
  }
}

2

Answers


  1. You can just call the async calls serially, since it waits for the result, hence it would execute serially.

    func execute() async throws {
        do {
            let createUser =  try await repository.createUser()
            let submit = try await repository.submitApplication()
            let completeRequest =  try await repository.submitMemorableWord()
        } catch {
            print("error " + error.localizedDescription)
        }
    }
    

    You can handle the error in any of the request in the catch block.

    Login or Signup to reply.
  2. It seems like it would be simpler to have execute manage the whole process

    func execute() async throws {
       let createUser = try await repository.createUser()
       let submit = try await repository.submitApplication()
       let completeRequest = try await repository.submitMemorableWord()
    }
    

    There is no need for the do/try/catch in execute(). Any errors will be thrown to the caller.

    If you want to be able to catch an error that tells you which part of the process failed, you can use a custom error that wraps the thrown error:

    enum RepositoryError: Error {
    
      case createUserError(_ error: Error)
      case submitApplicationError(_ error: Error)
      case submitWordError(_ error: Error)
    }
    
    func execute() async throws {
       do {
           let createUser = try await repository.createUser()
       catch {
           throw RespositoryError.createUserError(error)
       }
       do { 
           let submit = try await repository.submitApplication()
       } 
       catch {
           throw RespositoryError.submitApplicationError(error)
       }
       do {
           let completeRequest = try await repository.submitMemorableWord()
       }
       catch {
           throw RespositoryError.submitWordError(error)
       }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search