• Warnings
    • Unused disposable (unused-disposable)">Unused disposable (unused-disposable)
    • Unused observable sequence (unused-observable)">Unused observable sequence (unused-observable)

    Warnings

    Unused disposable (unused-disposable)

    The following is valid for the subscribe*, bind* and drive* family of functions that return Disposable.

    You will receive a warning for doing something such as this:

    1. let xs: Observable<E> ....
    2. xs
    3. .filter { ... }
    4. .map { ... }
    5. .switchLatest()
    6. .subscribe(onNext: {
    7. ...
    8. }, onError: {
    9. ...
    10. })

    The subscribe function returns a subscription Disposable that can be used to cancel computation and free resources. However, not using it (and thus not disposing it) will result in an error.

    The preferred way of terminating these fluent calls is by using a DisposeBag, either through chaining a call to .addDisposableTo(disposeBag) or by adding the disposable directly to the bag.

    1. let xs: Observable<E> ....
    2. let disposeBag = DisposeBag()
    3. xs
    4. .filter { ... }
    5. .map { ... }
    6. .switchLatest()
    7. .subscribe(onNext: {
    8. ...
    9. }, onError: {
    10. ...
    11. })
    12. .addDisposableTo(disposeBag) // <--- note `addDisposableTo`

    When disposeBag gets deallocated, the disposables contained within it will be automatically disposed as well.

    In the case where xs terminates in a predictable way with either a Completed or Error message, not handling the subscription Disposable won’t leak any resources. However, even in this case, using a dispose bag is still the preferred way to handle subscription disposables. It ensures that element computation is always terminated at a predictable moment, and makes your code robust and future proof because resources will be properly disposed even if the implementation of xs changes.

    Another way to make sure subscriptions and resources are tied to the lifetime of some object is by using the takeUntil operator.

    1. let xs: Observable<E> ....
    2. let someObject: NSObject ...
    3. _ = xs
    4. .filter { ... }
    5. .map { ... }
    6. .switchLatest()
    7. .takeUntil(someObject.rx_deallocated) // <-- note the `takeUntil` operator
    8. .subscribe(onNext: {
    9. ...
    10. }, onError: {
    11. ...
    12. })

    If ignoring the subscription Disposable is the desired behavior, this is how to silence the compiler warning.

    1. let xs: Observable<E> ....
    2. _ = xs // <-- note the underscore
    3. .filter { ... }
    4. .map { ... }
    5. .switchLatest()
    6. .subscribe(onNext: {
    7. ...
    8. }, onError: {
    9. ...
    10. })

    Unused observable sequence (unused-observable)

    You will receive a warning for doing something such as this:

    1. let xs: Observable<E> ....
    2. xs
    3. .filter { ... }
    4. .map { ... }

    This code defines an observable sequence that is filtered and mapped from the xs sequence but then ignores the result.

    Since this code just defines an observable sequence and then ignores it, it doesn’t actually do anything.

    Your intention was probably to either store the observable sequence definition and use it later …

    1. let xs: Observable<E> ....
    2. let ys = xs // <--- names definition as `ys`
    3. .filter { ... }
    4. .map { ... }

    … or start computation based on that definition

    1. let xs: Observable<E> ....
    2. let disposeBag = DisposeBag()
    3. xs
    4. .filter { ... }
    5. .map { ... }
    6. .subscribeNext { nextElement in // <-- note the `subscribe*` method
    7. // use the element
    8. print(nextElement)
    9. }
    10. .addDisposableTo(disposeBag)