본문 바로가기
iOS/Swift

RxSwift 객체 구독하기 (Observable)

by DnaJ 2019. 5. 18.
반응형

RxSwift  객체 구독하기 (Observable)

포스팅 하기에는 정말 민망할 정도로 자세하게 홈페이지에 설명이 작성되어 있다.

영어로 작성이 되어있지만 오른쪽에 한국어로 변경이 가능하도록 되어 있어 편하게 볼수 있다.

 

 

 

Observable : 식별할 수 있는, 관찰할 수 있는

 

http://reactivex.io/documentation/observable.html

 

ReactiveX - Observable

Observable In ReactiveX an observer subscribes to an Observable. Then that observer reacts to whatever item or sequence of items the Observable emits. This pattern facilitates concurrent operations because it does not need to block while waiting for the Ob

reactivex.io

 

 

RxSwift.io에 있는 그림이다. '마블 다이어그램' 으로 어느 블로그에서든 많이 보인다.

영어로 되어 있어 번역을 했다.

 

This is the timeline of the Observable. Time flows from left to right.

Observable의 타임 라인 입니다. 시간의 흐름은 왼쪽에서 오른쪽입니다.

 

These are items emitted by the Observable.

이것들은 Obervable이 방출하는 아이템들 입니다. 

 

This vertical line indicates that the Observable has completed successfully.

이 수직선은 Obsevable이 성공적으로 완료되었음을 나타냅니다.

 

These dotted lines and this box indicate that a transformation is being applied to the Observable. 

이 점선과 상자는 Observable에 변형이 적용되고 있음을 나타냅니다.

 

The text inside the box shows the nature of the transformation.

상자 안의 텍스트는 변환의 특성을 보여줍니다.

 

This Observable is the result of the transformation.

이 Observable은 변화에 대한 결과입니다.

 

If for some reson the Observable terminiates abnormally, with an error, the vertical line is replaced by an x.

Observable이 비정상적으로 종료되면 오류가 발생하여 수직선이 x로 대체됩니다.

 

위의 설명대로 Observable은 일련의 시퀀스다.

github에서는 Observable aka sequences라고 되어있다.

https://github.com/ReactiveX/RxSwift/blob/master/Documentation/GettingStarted.md#observables-aka-sequences

 

ReactiveX/RxSwift

Reactive Programming in Swift. Contribute to ReactiveX/RxSwift development by creating an account on GitHub.

github.com

 

Observable 사용

Define a method that does something useful with the return value from the asynchronous call; this method is part of the observer.

  1. Define the asynchronous call itself as an Observable.
  2. Attach the observer to that Observable by subscribing it (this also initiates the actions of the Observable).
  3. Go on with your business; whenever the call returns, the observer’s method will begin to operate on its return value or values — the items emitted by the Observable.
  1. Observable로 비동기 호출을 정의한다.
  2. 구독을 통해 옵저버를 Observable 객체에 연결 시킨다(또한, 동시에 Observable의 동작을 초기화 한다).
  3. 필요한 코드를 계속 구현한다; 메서드 호출로 결과가 리턴될 때마다, 옵저버의 메서드는 리턴 값 또는 (Observable이 배출하는)항목들을 사용해서 연산을 시작한다.

OnNext, OnError, OnComplete 

The Subscribe method is how you connect an observer to an Observable. Your observer implements some subset of the following methods:

Subscribe 메서드를 통해 옵저버와 Observable연결한다. 여러분의 옵저버는 아래의 메서드를 구현하게 될 것이다:

 

onNext

An Observable calls this method whenever the Observable emits an item. 

Observable은 새로운 항목들을 배출할 때마다 이 메서드를 호출한다.  메서드는 Observable이 배출하는 항목을 파라미터로 전달 받는다.

 

onError

An Observable calls this method to indicate that it has failed to generate the expected data or has encountered some other error. It will not make further calls to onNext or onCompleted. The onError method takes as its parameter an indication of what caused the error. 

Observable은 기대하는 데이터가 생성되지 않았거나 다른 이유로 오류가 발생할 경우 오류를 알리기 위해 이 메서드를 호출한다. 

이이 메서드가 호출되면 onNext나 onCompleted는 더 이상 호출되지 않는다. onError 메서드는 오류 정보를 저장하고 있는 객체를 파라미터로 전달 받는다.

 

onCompleted 

An Observable calls this method after it has called onNext for the final time, if it has not encountered any errors.

오류가 발생하지 않았다면 Observable은 마지막 onNext를 호출한 후 이 메서드를 호출한다.

 

반응형

댓글