RxSwift/README.md

211 lines
7.6 KiB
Markdown
Raw Normal View History

2016-01-27 00:05:54 +03:00
<img src="assets/Rx_Logo_M.png" alt="Miss Electric Eel 2016" width="36" height="36"> RxSwift: ReactiveX for Swift
======================================
[![Travis CI](https://travis-ci.org/ReactiveX/RxSwift.svg?branch=master)](https://travis-ci.org/ReactiveX/RxSwift) ![platforms](https://img.shields.io/badge/platforms-iOS%20%7C%20macOS%20%7C%20tvOS%20%7C%20watchOS%20%7C%20Linux-333333.svg) ![pod](https://img.shields.io/cocoapods/v/RxSwift.svg) [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) [![Swift Package Manager compatible](https://img.shields.io/badge/Swift%20Package%20Manager-compatible-brightgreen.svg)](https://github.com/apple/swift-package-manager)
2016-03-18 00:35:23 +03:00
2015-07-28 00:28:04 +03:00
Rx is a [generic abstraction of computation](https://youtu.be/looJcaeboBY) expressed through `Observable<Element>` interface.
This is a Swift version of [Rx](https://github.com/Reactive-Extensions/Rx.NET).
2016-10-07 14:00:51 +03:00
It tries to port as many concepts from the original version as possible, but some concepts were adapted for more pleasant and performant integration with iOS/macOS environment.
2015-07-28 00:28:04 +03:00
Cross platform documentation can be found on [ReactiveX.io](http://reactivex.io/).
Like the original Rx, its intention is to enable easy composition of asynchronous operations and event/data streams.
2015-07-28 00:28:04 +03:00
KVO observing, async operations and streams are all unified under [abstraction of sequence](Documentation/GettingStarted.md#observables-aka-sequences). This is the reason why Rx is so simple, elegant and powerful.
2016-02-14 17:10:14 +03:00
## I came here because I want to ...
2016-02-14 17:31:03 +03:00
###### ... understand
2016-02-14 17:29:12 +03:00
* [why use rx?](Documentation/Why.md)
2016-03-12 22:12:59 +03:00
* [the basics, getting started with RxSwift](Documentation/GettingStarted.md)
* [traits](Documentation/Traits.md) - what are `Single`, `Completable`, `Maybe`, `Driver`, `ControlProperty`, and `Variable` ... and why do they exist?
2016-03-12 22:12:59 +03:00
* [testing](Documentation/UnitTests.md)
* [tips and common errors](Documentation/Tips.md)
* [debugging](Documentation/GettingStarted.md#debugging)
2016-02-15 01:26:18 +03:00
* [the math behind Rx](Documentation/MathBehindRx.md)
* [what are hot and cold observable sequences?](Documentation/HotAndColdObservables.md)
2016-02-14 17:29:12 +03:00
###### ... install
2016-09-23 16:57:00 +03:00
* Integrate RxSwift/RxCocoa with my app. [Installation Guide](#installation)
2016-02-14 17:29:12 +03:00
###### ... hack around
2016-05-12 14:42:23 +03:00
* with the example app. [Running Example App](Documentation/ExampleApp.md)
2016-02-14 17:29:12 +03:00
* with operators in playgrounds. [Playgrounds](Documentation/Playgrounds.md)
2016-02-14 17:29:12 +03:00
###### ... interact
2017-02-11 14:56:51 +03:00
* All of this is great, but it would be nice to talk with other people using RxSwift and exchange experiences. <br />[![Slack channel](http://rxswift-slack.herokuapp.com/badge.svg)](http://rxswift-slack.herokuapp.com/) [Join Slack Channel](http://rxswift-slack.herokuapp.com)
2016-10-23 20:39:58 +03:00
* Report a problem using the library. [Open an Issue With Bug Template](.github/ISSUE_TEMPLATE.md)
2016-02-14 17:10:14 +03:00
* Request a new feature. [Open an Issue With Feature Request Template](Documentation/NewFeatureRequestTemplate.md)
2016-02-14 17:31:03 +03:00
###### ... compare
2015-07-28 00:28:04 +03:00
2016-02-15 01:26:18 +03:00
* [with other libraries](Documentation/ComparisonWithOtherLibraries.md).
2015-05-03 19:02:59 +03:00
2015-09-09 12:44:25 +03:00
2016-02-15 01:26:18 +03:00
###### ... find compatible
2015-09-09 12:44:25 +03:00
2016-02-15 01:26:18 +03:00
* libraries from [RxSwiftCommunity](https://github.com/RxSwiftCommunity).
* [Pods using RxSwift](https://cocoapods.org/?q=uses%3Arxswift).
2015-09-09 12:44:25 +03:00
2016-02-14 17:31:03 +03:00
###### ... see the broader vision
2015-09-09 12:44:25 +03:00
* Does this exist for Android? [RxJava](https://github.com/ReactiveX/RxJava)
2016-02-14 17:10:14 +03:00
* Where is all of this going, what is the future, what about reactive architectures, how do you design entire apps this way? [Cycle.js](https://github.com/cyclejs/cycle-core) - this is javascript, but [RxJS](https://github.com/Reactive-Extensions/RxJS) is javascript version of Rx.
2015-09-09 12:44:25 +03:00
2016-04-20 13:08:42 +03:00
## Usage
2016-04-20 14:39:16 +03:00
2016-04-20 13:48:31 +03:00
<table>
<tr>
<th width="30%">Here's an example</th>
<th width="30%">In Action</th>
</tr>
<tr>
2016-04-20 14:39:16 +03:00
<td>Define search for GitHub repositories ...</td>
2016-04-20 13:48:31 +03:00
<th rowspan="9"><img src="https://raw.githubusercontent.com/kzaher/rxswiftcontent/master/GithubSearch.gif"></th>
</tr>
<tr>
<td><div class="highlight highlight-source-swift"><pre>
let searchResults = searchBar.rx.text.orEmpty
2016-04-20 13:08:42 +03:00
.throttle(0.3, scheduler: MainScheduler.instance)
.distinctUntilChanged()
2017-03-20 03:18:34 +03:00
.flatMapLatest { query -> Observable&lt;[Repository]&gt; in
2016-12-17 19:12:47 +03:00
if query.isEmpty {
return .just([])
}
2016-04-20 13:08:42 +03:00
return searchGitHub(query)
.catchErrorJustReturn([])
}
2016-04-20 13:48:31 +03:00
.observeOn(MainScheduler.instance)</pre></div></td>
</tr>
<tr>
2016-04-20 14:39:16 +03:00
<td>... then bind the results to your tableview</td>
2016-04-20 13:48:31 +03:00
</tr>
<tr>
<td width="30%"><div class="highlight highlight-source-swift"><pre>
2016-04-20 13:08:42 +03:00
searchResults
.bind(to: tableView.rx.items(cellIdentifier: "Cell")) {
2016-04-20 13:48:31 +03:00
(index, repository: Repository, cell) in
2016-04-20 13:08:42 +03:00
cell.textLabel?.text = repository.name
cell.detailTextLabel?.text = repository.url
}
.disposed(by: disposeBag)</pre></div></td>
2016-04-20 13:48:31 +03:00
</tr>
</table>
2016-04-20 13:08:42 +03:00
## Requirements
* Xcode 8.0
* Swift 3.0
2017-06-14 11:34:21 +03:00
* Swift 2.3 ([use `rxswift-2.0` branch](https://github.com/ReactiveX/RxSwift/tree/rxswift-2.0) instead)
2016-04-20 13:08:42 +03:00
## Installation
Rx doesn't contain any external dependencies.
2016-05-12 14:42:23 +03:00
These are currently the supported options:
2016-04-20 13:08:42 +03:00
### Manual
2016-05-12 14:42:23 +03:00
Open Rx.xcworkspace, choose `RxExample` and hit run. This method will build everything and run the sample app
2016-04-20 13:08:42 +03:00
### [CocoaPods](https://guides.cocoapods.org/using/using-cocoapods.html)
2016-10-23 20:39:05 +03:00
**Tested with `pod --version`: `1.1.1`**
2017-02-13 11:44:24 +03:00
```ruby
2016-04-20 13:08:42 +03:00
# Podfile
use_frameworks!
target 'YOUR_TARGET_NAME' do
2016-10-23 20:39:05 +03:00
pod 'RxSwift', '~> 3.0'
pod 'RxCocoa', '~> 3.0'
end
2016-05-12 14:42:23 +03:00
# RxTests and RxBlocking make the most sense in the context of unit/integration tests
target 'YOUR_TESTING_TARGET' do
2016-10-23 20:39:05 +03:00
pod 'RxBlocking', '~> 3.0'
pod 'RxTest', '~> 3.0'
2016-04-20 13:08:42 +03:00
end
```
2016-05-12 14:42:23 +03:00
Replace `YOUR_TARGET_NAME` and then, in the `Podfile` directory, type:
2016-04-20 13:08:42 +03:00
2017-02-13 11:44:24 +03:00
```bash
2016-04-20 13:08:42 +03:00
$ pod install
```
### [Carthage](https://github.com/Carthage/Carthage)
2016-10-23 20:39:05 +03:00
**Tested with `carthage version`: `0.18.1`**
2016-04-20 13:08:42 +03:00
Add this to `Cartfile`
```
2016-10-23 20:39:05 +03:00
github "ReactiveX/RxSwift" ~> 3.0
2016-04-20 13:08:42 +03:00
```
2017-02-13 11:44:24 +03:00
```bash
2016-04-20 13:08:42 +03:00
$ carthage update
```
### [Swift Package Manager](https://github.com/apple/swift-package-manager)
2016-10-23 20:39:05 +03:00
**Tested with `swift build --version`: `3.0.0 (swiftpm-19)`**
Create a `Package.swift` file.
2017-02-13 11:44:24 +03:00
```swift
import PackageDescription
let package = Package(
name: "RxTestProject",
targets: [],
dependencies: [
2016-10-23 20:39:05 +03:00
.Package(url: "https://github.com/ReactiveX/RxSwift.git", majorVersion: 3)
]
)
```
2017-02-13 11:44:24 +03:00
```bash
2016-10-27 23:31:44 +03:00
$ swift build
```
To build or test a module with RxTest dependency, set `TEST=1`. ([RxSwift >= 3.4.2](https://github.com/ReactiveX/RxSwift/releases/tag/3.4.2))
```bash
$ TEST=1 swift test
```
2016-04-20 13:08:42 +03:00
### Manually using git submodules
* Add RxSwift as a submodule
2017-02-13 11:44:24 +03:00
```bash
2016-04-20 13:08:42 +03:00
$ git submodule add git@github.com:ReactiveX/RxSwift.git
```
* Drag `Rx.xcodeproj` into Project Navigator
* Go to `Project > Targets > Build Phases > Link Binary With Libraries`, click `+` and select `RxSwift-[Platform]` and `RxCocoa-[Platform]` targets
## References
2015-05-03 19:02:59 +03:00
2015-07-28 00:28:04 +03:00
* [http://reactivex.io/](http://reactivex.io/)
2015-05-03 19:02:59 +03:00
* [Reactive Extensions GitHub (GitHub)](https://github.com/Reactive-Extensions)
2017-06-14 10:07:47 +03:00
* [RxSwift RayWenderlich.com Book](https://store.raywenderlich.com/products/rxswift)
* [Boxue.io RxSwift Online Course](https://boxueio.com/series/rxswift-101) (Chinese 🇨🇳)
2015-05-03 19:02:59 +03:00
* [Erik Meijer (Wikipedia)](http://en.wikipedia.org/wiki/Erik_Meijer_%28computer_scientist%29)
2015-07-28 00:28:04 +03:00
* [Expert to Expert: Brian Beckman and Erik Meijer - Inside the .NET Reactive Framework (Rx) (video)](https://youtu.be/looJcaeboBY)
* [Reactive Programming Overview (Jafar Husain from Netflix)](https://www.youtube.com/watch?v=dwP1TNXE6fc)
2015-05-03 19:02:59 +03:00
* [Subject/Observer is Dual to Iterator (paper)](http://csl.stanford.edu/~christos/pldi2010.fit/meijer.duality.pdf)
* [Rx standard sequence operators visualized (visualization tool)](http://rxmarbles.com/)
* [Haskell](https://www.haskell.org/)