13
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RxSwiftに新しく追加されたSingle, Maybe, Completable

Last updated at Posted at 2017-04-17

RxSwift 3.3.0でRxJavaにインスパイアされた SingleMaybeCompletable が追加されたので、使い所をかんがえて見た。

Single

性質

The 2.x Single reactive base type, which can emit a single onSuccess or onError has been redesigned from scratch.

1度だけ onSuccsess または onError が流れる。 onCompleteonNext は流れない点に注意

RxSwift
public enum SingleEvent<Element> {
    /// One and only sequence element is produced. (underlying observable sequence emits: `.next(Element)`, `.completed`)
    case success(Element)

    /// Sequence terminated with an error. (underlying observable sequence emits: `.error(Error)`)
    case error(Swift.Error)
}

使い所

  • レスポンスのあるAPIリクエスト
  • プロミス的な使い方
  • FirebaseのsingleEvent

Completable

性質

1度だけonComplete または onError が流れる。

RxSwift
public enum CompletableEvent {
    /// Sequence terminated with an error. (underlying observable sequence emits: `.error(Error)`)
    case error(Swift.Error)

    /// Sequence completed successfully.
    case completed
}

使い所

  • レスポンスボディの無いAPIリクエスト

Maybe

性質

Conceptually, it is a union of Single and Completable providing the means to capture an emission pattern where there could be 0 or 1 item or an error signalled by some reactive source.

SingleとCompleteが合わさったような性質。
1度だけ、アイテムかerrorが流れるか、または全くイベントが流れない。
(実装の際にはイベントが流れてこないことがある点も考慮が必要)

使い所

  • 1回だけ表示したいイベントの条件とか?

所感

今までは1度しか流れてこないイベントもObservableで流しっぱなしで、
Sigle、Completableと1回しか流れてこないように明示的にできるのはとても良いなー( ˘ω˘)

参考

Release Hashimoto · ReactiveX/RxSwift
What's different in 2.0 · ReactiveX/RxJava Wiki
ReactiveX - Single

13
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?