Ways to do callback, when and where

I’ve recently started to do some more complicated programming with network-related work on iOS with Swift.
This introduced a lot of asynchronous code not only in networking but also some exchange of information between view controllers.

Anyway, my question is simple, and can be quite general. As far as I know, there are (at least) four ways to do callback, so is there a general rule as to which is better in each case?

  • callback closure (function) as a function argument
  • delegation with protocol
  • notification (or observer)
  • shared state object

Use cases in my application

  • Network stuff, POST request or download file
  • passing data one-way between view controllers on button tap
  • passing data both-ways between view controllers
    (One view shows the selected option, another view gives the real options for user to select)

1

Here are my views on the various observation patterns. I may update this list over time if comments seem to require it.

  1. Callback with closure: This is great if you have some sort of
    one-shot thing (like a network response), or multiple responses that all need to be handled the same way. Not so good if you are expecting multiple responses which need to be handled differently or different types of
    responses (which would require a different closure for each type of
    response.) Also not helpful if you have multiple objects interested
    in the response.
  2. Delegation with protocol: Makes a lot of sense if there is a
    business reason why there should only be one listener/observer and
    it makes sense for that observer to handle several different state
    changes. In other words, if you have more than one object that is
    interested in the subject, then delegation won’t work. If you end up
    with a protocol that only forces the implementation of one method,
    then this pattern is likely not a good idea either. Also, if you
    have an observer that wants to track several different objects that
    implement the protocol in question, then you should probably
    reconsider the use of this pattern.
  3. Notification: The NSNotificationCenter works pretty well for very
    generic or system wide messages. If you only have one observer, or
    only expect the message to be sent once, then this is a poor choice
    of pattern. Also, this pattern can save you a lot of refactoring
    when you have to send a message between two disparate objects in a
    mature system (i.e., a hack.)
  4. Key value Observation: This works well if the observer only wants to
    monitor the state of a single field but can get unwieldy if trying
    to observe several fields or if you just want to be informed of some
    signal.

Some patterns you didn’t mention:

  1. target/action callback: An example of this is UIControl… This has
    the same benefits and tradeoffs as a callback with closure and I
    think has largely been superseded by it.
  2. Abstract method/subclass instantiation: There is the classic mode of
    implementing a method in a subclass to handle requirements of the
    base class. UIViewController’s viewDidLoad and the like are
    examples. Another pattern that has been largely superseded, by
    protocols, but comes in handy if you happen to already have the
    class structure in place.
  3. Futures/Promises: This pattern has the same benefits and drawbacks as callback closures, but can avoid nested closures that sometimes pop-up.

Lastly, there is reactive programming (ReactiveCocoa and RxSwift for example) that can potentially take the place of all of the above patterns. It takes some time to learn but once you get the hang of it, you can handle all your observation needs with one pattern.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật