Skip to content

Commit

Permalink
Update changed all project uses of old Disposable API to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
andyyhope committed Jan 22, 2017
1 parent e5d6328 commit 4e2498a
Show file tree
Hide file tree
Showing 40 changed files with 163 additions and 163 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Expand Up @@ -105,13 +105,13 @@ let text: Observable<String> = Observable.just("")
// Previously `map { $0 }` was needed because of mismatch betweeen sequence `String` type and `String?` type
// on binding `rx.text` observer.
text.bindTo(label.rx.text)
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)

...

let text = Driver.just("")
text.drive(label.rx.text)
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)
```

* Adds trim output parameter to `debug` operator. #930
Expand Down
2 changes: 1 addition & 1 deletion Documentation/Examples.md
Expand Up @@ -174,7 +174,7 @@ self.usernameOutlet.rx.text
// pending async operations.
// Instead of doing it manually, which is tedious,
// let's dispose everything automagically upon view controller dealloc.
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)
```

It doesn't get any simpler than that. There are [more examples](../RxExample) in the repository, so feel free to check them out.
Expand Down
6 changes: 3 additions & 3 deletions Documentation/GettingStarted.md
Expand Up @@ -97,7 +97,7 @@ protocol ObserverType {

**To cancel production of sequence elements and free resources immediately, call `dispose` on the returned subscription.**

If a sequence terminates in finite time, not calling `dispose` or not using `addDisposableTo(disposeBag)` won't cause any permanent resource leaks. However, those resources will be used until the sequence completes, either by finishing production of elements or returning an error.
If a sequence terminates in finite time, not calling `dispose` or not using `disposed(by: disposeBag)` won't cause any permanent resource leaks. However, those resources will be used until the sequence completes, either by finishing production of elements or returning an error.

If a sequence does not terminate in some way, resources will be allocated permanently unless `dispose` is called manually, automatically inside of a `disposeBag`, `takeUntil` or in some other way.

Expand Down Expand Up @@ -670,7 +670,7 @@ This isn't something that should be practiced often, and is a bad code smell, bu
.subscribe(onNext: { being in // exit the Rx monad
self.doSomeStateMagic(being)
})
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)

//
// Mess
Expand Down Expand Up @@ -700,7 +700,7 @@ Every time you do this, somebody will probably write this code somewhere
.subscribe(onNext: { kitten in
// so something with kitten
})
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)
```

so please try not to do this.
Expand Down
4 changes: 2 additions & 2 deletions Documentation/Migration.md
Expand Up @@ -11,7 +11,7 @@ The migration should be pretty straightforward. Changes are mostly cosmetic, so
* Find replace all `empty` to `Observable.empty`
* Since we've moved from `>-` to `.`, free functions are now methods, so use `.switchLatest()`, `.distinctUntilChanged()`, ... instead of `>- switchLatest`, `>- distinctUntilChanged`
* We've moved from free functions to extensions so it's now `[a, b, c].concat()`, `.merge()`, ... instead of `concat([a, b, c])`, `merge(sequences)`
* Similarly, it's now `subscribe { n in ... }.addDisposableTo(disposeBag)` instead of `>- disposeBag.addDisposable`
* Similarly, it's now `subscribe { n in ... }.disposed(by: disposeBag)` instead of `>- disposeBag.addDisposable`
* The method `next` on `Variable` is now `value` setter
* If you want to use `UITableView` and/or `UICollectionView`, this is the basic use case now:

Expand All @@ -20,7 +20,7 @@ viewModel.rows
.bindTo(resultsTableView.rx_itemsWithCellIdentifier("WikipediaSearchCell", cellType: WikipediaSearchCell.self)) { (_, viewModel, cell) in
cell.viewModel = viewModel
}
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)
```

If you have any doubts about how some concept in RxSwift 2.0 works, check out the [Example app](../RxExample) or playgrounds.
12 changes: 6 additions & 6 deletions Documentation/Units.md
Expand Up @@ -173,13 +173,13 @@ let results = query.rx.text
results
.map { "\($0.count)" }
.bindTo(resultCount.rx.text)
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)

results
.bindTo(resultsTableView.rx.items(cellIdentifier: "Cell")) { (_, result, cell) in
cell.textLabel?.text = "\(result)"
}
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)
```

The intended behavior of this code was to:
Expand Down Expand Up @@ -208,13 +208,13 @@ let results = query.rx.text
results
.map { "\($0.count)" }
.bindTo(resultCount.rx.text)
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)

results
.bindTo(resultsTableView.rx.items(cellIdentifier: "Cell")) { (_, result, cell) in
cell.textLabel?.text = "\(result)"
}
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)
```

Making sure all of these requirements are properly handled in large systems can be challenging, but there is a simpler way of using the compiler and units to prove these requirements are met.
Expand All @@ -232,13 +232,13 @@ let results = query.rx.text.asDriver() // This converts a normal sequence
results
.map { "\($0.count)" }
.drive(resultCount.rx.text) // If there is a `drive` method available instead of `bindTo`,
.addDisposableTo(disposeBag) // that means that the compiler has proven that all properties
.disposed(by: disposeBag) // that means that the compiler has proven that all properties
// are satisfied.
results
.drive(resultsTableView.rx.items(cellIdentifier: "Cell")) { (_, result, cell) in
cell.textLabel?.text = "\(result)"
}
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)
```

So what is happening here?
Expand Down
6 changes: 3 additions & 3 deletions Documentation/Warnings.md
Expand Up @@ -23,7 +23,7 @@ xs

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.
The preferred way of terminating these fluent calls is by using a `DisposeBag`, either through chaining a call to `.disposed(by: disposeBag)` or by adding the disposable directly to the bag.

```Swift
let xs: Observable<E> ....
Expand All @@ -38,7 +38,7 @@ xs
}, onError: {
...
})
.addDisposableTo(disposeBag) // <--- note `addDisposableTo`
.disposed(by: disposeBag) // <--- note `addDisposableTo`
```

When `disposeBag` gets deallocated, the disposables contained within it will be automatically disposed as well.
Expand Down Expand Up @@ -118,5 +118,5 @@ xs
// use the element
print(nextElement)
})
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)
```
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -102,7 +102,7 @@ searchResults
cell.textLabel?.text = repository.name
cell.detailTextLabel?.text = repository.url
}
.addDisposableTo(disposeBag)</pre></div></td>
.disposed(by: disposeBag)</pre></div></td>
</tr>
</table>

Expand Down
Expand Up @@ -23,7 +23,7 @@ example("startWith") {
.startWith("2️⃣")
.startWith("3️⃣", "🅰️", "🅱️")
.subscribe(onNext: { print($0) })
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)
}
/*:
> As this example demonstrates, `startWith` can be chained on a last-in-first-out basis, i.e., each successive `startWith`'s elements will be prepended before the prior `startWith`'s elements.
Expand All @@ -41,7 +41,7 @@ example("merge") {
Observable.of(subject1, subject2)
.merge()
.subscribe(onNext: { print($0) })
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)

subject1.onNext("🅰️")

Expand Down Expand Up @@ -71,7 +71,7 @@ example("zip") {
"\(stringElement) \(intElement)"
}
.subscribe(onNext: { print($0) })
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)

stringSubject.onNext("🅰️")
stringSubject.onNext("🅱️")
Expand Down Expand Up @@ -99,7 +99,7 @@ example("combineLatest") {
"\(stringElement) \(intElement)"
}
.subscribe(onNext: { print($0) })
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)

stringSubject.onNext("🅰️")

Expand All @@ -122,7 +122,7 @@ example("Array.combineLatest") {
"\($0[0]) \($0[1]) \($0[2])"
}
.subscribe(onNext: { print($0) })
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)
}
/*:
> Because the `combineLatest` variant that takes a collection passes an array of values to the selector function, it requires that all source `Observable` sequences are of the same type.
Expand All @@ -142,7 +142,7 @@ example("switchLatest") {
variable.asObservable()
.switchLatest()
.subscribe(onNext: { print($0) })
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)

subject1.onNext("🏈")
subject1.onNext("🏀")
Expand Down
Expand Up @@ -23,7 +23,7 @@ example("never") {
print("This will never be printed")
}

neverSequenceSubscription.addDisposableTo(disposeBag)
neverSequenceSubscription.disposed(by: disposeBag)
}
/*:
----
Expand All @@ -37,7 +37,7 @@ example("empty") {
.subscribe { event in
print(event)
}
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)
}
/*:
> This example also introduces chaining together creating and subscribing to an `Observable` sequence.
Expand All @@ -52,7 +52,7 @@ example("just") {
.subscribe { event in
print(event)
}
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)
}
/*:
----
Expand All @@ -66,7 +66,7 @@ example("of") {
.subscribe(onNext: { element in
print(element)
})
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)
}
/*:
> This example also introduces using the `subscribe(onNext:)` convenience method. Unlike `subscribe(_:)`, which subscribes an _event_ handler for all event types (Next, Error, and Completed), `subscribe(onNext:)` subscribes an _element_ handler that will ignore Error and Completed events and only produce Next event elements. There are also `subscribe(onError:)` and `subscribe(onCompleted:)` convenience methods, should you only want to subscribe to those event types. And there is a `subscribe(onNext:onError:onCompleted:onDisposed:)` method, which allows you to react to one or more event types and when the subscription is terminated for any reason, or disposed, in a single call:
Expand All @@ -87,7 +87,7 @@ example("from") {

Observable.from(["🐶", "🐱", "🐭", "🐹"])
.subscribe(onNext: { print($0) })
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)
}
/*:
> This example also demonstrates using the default argument name `$0` instead of explicitly naming the argument.
Expand All @@ -108,7 +108,7 @@ example("create") {

myJust("🔴")
.subscribe { print($0) }
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)
}
/*:
----
Expand All @@ -120,7 +120,7 @@ example("range") {

Observable.range(start: 1, count: 10)
.subscribe { print($0) }
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)
}
/*:
----
Expand All @@ -133,7 +133,7 @@ example("repeatElement") {
Observable.repeatElement("🔴")
.take(3)
.subscribe(onNext: { print($0) })
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)
}
/*:
> This example also introduces using the `take` operator to return a specified number of elements from the start of a sequence.
Expand All @@ -150,7 +150,7 @@ example("generate") {
iterate: { $0 + 1 }
)
.subscribe(onNext: { print($0) })
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)
}
/*:
----
Expand All @@ -176,11 +176,11 @@ example("deferred") {

deferredSequence
.subscribe(onNext: { print($0) })
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)

deferredSequence
.subscribe(onNext: { print($0) })
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)
}
/*:
----
Expand All @@ -192,7 +192,7 @@ example("error") {

Observable<Int>.error(TestError.test)
.subscribe { print($0) }
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)
}
/*:
----
Expand All @@ -205,7 +205,7 @@ example("doOn") {
Observable.of("🍎", "🍐", "🍊", "🍋")
.do(onNext: { print("Intercepted:", $0) }, onError: { print("Intercepted error:", $0) }, onCompleted: { print("Completed") })
.subscribe(onNext: { print($0) })
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)
}
//: > There are also `doOnNext(_:)`, `doOnError(_:)`, and `doOnCompleted(_:)` convenience methods to intercept those specific events, and `doOn(onNext:onError:onCompleted:)` to intercept one or more events in a single call.

Expand Down
Expand Up @@ -41,7 +41,7 @@ example("debug") {
.retry(3)
.debug()
.subscribe(onNext: { print($0) })
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)
}
/*:
----
Expand Down
Expand Up @@ -23,7 +23,7 @@ example("catchErrorJustReturn") {
sequenceThatFails
.catchErrorJustReturn("😊")
.subscribe { print($0) }
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)

sequenceThatFails.onNext("😬")
sequenceThatFails.onNext("😨")
Expand All @@ -49,7 +49,7 @@ example("catchError") {
return recoverySequence
}
.subscribe { print($0) }
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)

sequenceThatFails.onNext("😬")
sequenceThatFails.onNext("😨")
Expand Down Expand Up @@ -91,7 +91,7 @@ example("retry") {
sequenceThatErrors
.retry()
.subscribe(onNext: { print($0) })
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)
}
/*:
----
Expand Down Expand Up @@ -125,7 +125,7 @@ example("retry maxAttemptCount") {
sequenceThatErrors
.retry(3)
.subscribe(onNext: { print($0) })
.addDisposableTo(disposeBag)
.disposed(by: disposeBag)
}

//: [Next](@next) - [Table of Contents](Table_of_Contents)

0 comments on commit 4e2498a

Please sign in to comment.