UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.