LoginSignup
7
7

More than 5 years have passed since last update.

【Swift】RxSwiftのPlaygroundを読む⑨

Posted at

RxSwiftのPlaygroundを読んでみるシリーズ最終回は
デバッグ処理についてです。
(Debugging_Operators)

Debugging Operators

ログを出力する等、デバッグ時に有用なオペレータです。

debug

subscribeやeventまた、disposeなど
発生した状況に対しログを出力する。

let disposeBag = DisposeBag()

let debug = PublishSubject<String>()

debug
    .debug()
    .subscribe {}
    .addDisposableTo(disposeBag)

debug.onNext("🍑")
debug.onCompleted()

結果

2016-08-01 19:43:25.051: playground48.swift:12 (__lldb_expr_48) -> subscribed
2016-08-01 19:43:25.053: playground48.swift:12 (__lldb_expr_48) -> Event Next(🍑)
2016-08-01 19:43:25.055: playground48.swift:12 (__lldb_expr_48) -> Event Completed


let disposeBag = DisposeBag()

extension String: ErrorType {}

let debug = PublishSubject<String>()

debug
    .debug()
    .subscribe {}
    .addDisposableTo(disposeBag)

debug.onError("An error occurred.")

結果

2016-08-01 19:48:46.615: playground50.swift:12 (__lldb_expr_50) -> subscribed
2016-08-01 19:48:46.616: playground50.swift:12 (__lldb_expr_50) -> Event Error(An error occurred.)


let debug = PublishSubject<String>()

debug
    .debug()
    .subscribe {}.dispose()

結果

2016-08-01 19:50:42.255: playground52.swift:10 (__lldb_expr_52) -> subscribed
2016-08-01 19:50:42.257: playground52.swift:10 (__lldb_expr_52) -> disposed

RxSwift.resourceCount

Rx関連のリソースの数を返す。
メモリリークのチェック等に使える。

_ = {

    print(RxSwift.resourceCount)

    let disposeBag = DisposeBag()
    print(RxSwift.resourceCount)

    let variable = Variable("🍑")
    print(RxSwift.resourceCount)

    let subscription1 = variable.asObservable().subscribeNext { _ in }
    print(RxSwift.resourceCount)

    let subscription2 = variable.asObservable().subscribeNext { _ in }
    print(RxSwift.resourceCount)

    subscription1.dispose()
    print(RxSwift.resourceCount)

    subscription2.dispose()
    print(RxSwift.resourceCount)

}()

print(RxSwift.resourceCount)

結果

0
1
2
3
4
3
2
0

おわりに

以上RxSwiftのプレイグラウンドを読んでみましたシリーズ終了です。
これだけでは表面的な動きがいくつかわかったにすぎないので、
サンプルプログラム等を参考にさらに理解を深めていきたいと思います。

参考

7
7
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
7
7