상세 컨텐츠

본문 제목

Combine. Ch 1. Hello, Combine!

TIL/Combine

by box-jeon 2020. 4. 26. 21:11

본문

회사 동료들과 이야기 나누다가 충동적으로 구매하고 말았습니다. 프로젝트에 실제로 사용할 수 있으려면 시간이 좀 더 필요하겠지만... 구매는 충동적으로 이루어졌는데 학습은 충동적으로 못한다면 우스운 일입니다.

https://store.raywenderlich.com/products/combine-asynchronous-programming-with-swift

 

Combine: Asynchronous Programming with Swift

The best book for learning how to manage declarative asynchronous event handling with the Swift Combine framework!

store.raywenderlich.com

The Combine framework provides a declarative approach for how your app processes events. Rather than potentially implementing multiple delegate callbacks or completion handler closures, you can create a single processing chain for a given event source. Each part of the chain is a Combine operator that performs a distinct action on the elements received from the previous step.

Asynchronous programming in Foundation and UIKit/AppKit 

  • NotificationCenter
  • The delegate pattern
  • Grand Central Dispatch and Operations
  • Closures

Combine basics

  • Publishers: 세 가지 타입의 이벤트를 방출합니다. 제네릭 타입인 Output을 방출하거나 successful completion 또는 error completion을 방출합니다.
  • Operators: Publisher 프로토콜에 정의되어 있는 메소드들로 해당 Publisher를 리턴하거나 새로운 Publisher를 리턴하는 메소드들입니다. 이를 이용해 여러 개의 Operator들을 연결해서 사용할 수 있습니다.
  • Subscribers: 방출된 이벤트를 받아서 처리하는 주체입니다. Subscription chain의 맨 마지막 부분에 위치합니다. sink와 assign 함수가 빌트인으로 제공됩니다.

What's the benefit of Combine code over "standard" code?

  • Combine은 시스템 레벨에 통합되어 있습니다. 즉, Combine은 우리가 직접 구현하기 어려운 비공개 API들을 사용하고 있습니다.
  • Delegate이나 IBAction, closures 같은 기존 방식의 asynchronous code들은 추가적인 코드가 요구되고 이들을 위한 테스트를 작성하기 위해서도 많은 코드가 필요합니다. Combine은 코드 내 모든 asynchronous 동작들을 operator로 추상화하고 있으며, 이들은 이미 충분히 테스트되어 있습니다.
  • 만약 모든 asynchronous 동작들이 Publisher 인터페이스를 따른다면, 그들을 조합하고 재사용할 수 있게 됩니다.
  • Combine의 operator들은 자유자재로 조합이 가능해서, 새로운 operator를 만들어도 기존의 Combine 코드에 간단히 연결할 수 있습니다.
  • Asynchronous 코드를 테스팅하는 것은 무척 까다로운 일입니다. 하지만 Combine을 사용할 경우 asynchronous operators들은 이미 테스트되어 있기 때문에, 비즈니스 로직을 테스팅하는 데 집중할 수 있습니다.

'TIL > Combine' 카테고리의 다른 글

Combine. Ch 4. Filtering Operators  (0) 2020.08.23
Combine. Ch 3. Transforming Operators  (0) 2020.08.17
Combine. Ch 2. Publishers & Subscribers  (0) 2020.05.06

관련글 더보기

댓글 영역