TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

escaping closures vs delegates

Forums > Swift

Hi, I’m making an app for fun and I want to test some things; and I have a really trivial to ask the community: let’s say I have a simple GET request to receive the day of the week, and after receiving the result I have to update my view.

To handle that (simple) case from a viewModel, you prefer using a delegate or using a escaping closure ? And why ?

For example, with completion handler:

class MyViewController: UIViewController {
    var day: String
    let viewModel: MyViewModel

    override func viewDidLoad() {
        super.viewDidLoad()

        viewModel.getDay = { day in
            guard let unwrappedDay = day else { return }
            self.day = unwrappedDay
        }
    }
}

class MyViewModel {
    func getDay(completion: @escaping ((_ day: String?) -> Void)) {
        Service.getDay { result in
                completion(result.day)
            }
        }
    }
}

And with delegate:

class MyViewController: UIViewController, MyViewModelDelegate {
    var day: String
    let viewModel: MyViewModel

    override func viewDidLoad() {
        super.viewDidLoad()
        viewModel.delegate = self
        viewModel.load()
    }

    func updateDay(day: String) {
        self.day = day
    }
}

protocol MyViewModelDelegate {
    func updateDay(day: String)
}

class MyViewModel {
    weak var delegate: MyViewModelDelegate?

    func load() {
        Service.getDay { result in
                self.delegate?.updateDay(day: result.day)
            }
        }
    }
}

I’ve always used the first because it’s less to code and I find it more readable but I know a lot of people use the second way and I feel like I’m missing something !

Thanks 😀

2      

Escaping closure, for two main reasons:

  1. The code to handle the completion is right there where it is needed and you don't have to go read the delegate object's code to find out what's going to happen.
  2. Unless there are other objects that can also make use of the same type of delegate, I feel it's a waste to have to go through all the process of creating a protocol and making other objects conform to it.

3      

Ok, these are some good points and pretty much why I use escaping closure; thanks for your answer !

I'm still interested to know why some people prefers delegates thought

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.