You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

217 lines
8.1 KiB

6 years ago
  1. <img src="https://raw.githubusercontent.com/ReactiveX/RxSwift/master/assets/Rx_Logo_M.png" alt="Miss Electric Eel 2016" width="36" height="36"> RxSwift: ReactiveX for Swift
  2. ======================================
  3. [![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)](https://cocoapods.org/pods/RxSwift) [![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)
  4. * RxSwift 3.x / Swift 3.x can be found in [**rxswift-3.0** branch](https://github.com/ReactiveX/RxSwift/tree/rxswift-3.0).
  5. Rx is a [generic abstraction of computation](https://youtu.be/looJcaeboBY) expressed through `Observable<Element>` interface.
  6. This is a Swift version of [Rx](https://github.com/Reactive-Extensions/Rx.NET).
  7. 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.
  8. Cross platform documentation can be found on [ReactiveX.io](http://reactivex.io/).
  9. Like the original Rx, its intention is to enable easy composition of asynchronous operations and event/data streams.
  10. 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.
  11. ## I came here because I want to ...
  12. ###### ... understand
  13. * [why use rx?](Documentation/Why.md)
  14. * [the basics, getting started with RxSwift](Documentation/GettingStarted.md)
  15. * [traits](Documentation/Traits.md) - what are `Single`, `Completable`, `Maybe`, `Driver`, and `ControlProperty` ... and why do they exist?
  16. * [testing](Documentation/UnitTests.md)
  17. * [tips and common errors](Documentation/Tips.md)
  18. * [debugging](Documentation/GettingStarted.md#debugging)
  19. * [the math behind Rx](Documentation/MathBehindRx.md)
  20. * [what are hot and cold observable sequences?](Documentation/HotAndColdObservables.md)
  21. ###### ... install
  22. * Integrate RxSwift/RxCocoa with my app. [Installation Guide](#installation)
  23. ###### ... hack around
  24. * with the example app. [Running Example App](Documentation/ExampleApp.md)
  25. * with operators in playgrounds. [Playgrounds](Documentation/Playgrounds.md)
  26. ###### ... interact
  27. * 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://slack.rxswift.org) [Join Slack Channel](http://slack.rxswift.org)
  28. * Report a problem using the library. [Open an Issue With Bug Template](.github/ISSUE_TEMPLATE.md)
  29. * Request a new feature. [Open an Issue With Feature Request Template](Documentation/NewFeatureRequestTemplate.md)
  30. * Help out [Check out contribution guide](CONTRIBUTING.md)
  31. ###### ... compare
  32. * [with other libraries](Documentation/ComparisonWithOtherLibraries.md).
  33. ###### ... find compatible
  34. * libraries from [RxSwiftCommunity](https://github.com/RxSwiftCommunity).
  35. * [Pods using RxSwift](https://cocoapods.org/?q=uses%3Arxswift).
  36. ###### ... see the broader vision
  37. * Does this exist for Android? [RxJava](https://github.com/ReactiveX/RxJava)
  38. * 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.
  39. ## Usage
  40. <table>
  41. <tr>
  42. <th width="30%">Here's an example</th>
  43. <th width="30%">In Action</th>
  44. </tr>
  45. <tr>
  46. <td>Define search for GitHub repositories ...</td>
  47. <th rowspan="9"><img src="https://raw.githubusercontent.com/kzaher/rxswiftcontent/master/GithubSearch.gif"></th>
  48. </tr>
  49. <tr>
  50. <td><div class="highlight highlight-source-swift"><pre>
  51. let searchResults = searchBar.rx.text.orEmpty
  52. .throttle(0.3, scheduler: MainScheduler.instance)
  53. .distinctUntilChanged()
  54. .flatMapLatest { query -> Observable&lt;[Repository]&gt; in
  55. if query.isEmpty {
  56. return .just([])
  57. }
  58. return searchGitHub(query)
  59. .catchErrorJustReturn([])
  60. }
  61. .observeOn(MainScheduler.instance)</pre></div></td>
  62. </tr>
  63. <tr>
  64. <td>... then bind the results to your tableview</td>
  65. </tr>
  66. <tr>
  67. <td width="30%"><div class="highlight highlight-source-swift"><pre>
  68. searchResults
  69. .bind(to: tableView.rx.items(cellIdentifier: "Cell")) {
  70. (index, repository: Repository, cell) in
  71. cell.textLabel?.text = repository.name
  72. cell.detailTextLabel?.text = repository.url
  73. }
  74. .disposed(by: disposeBag)</pre></div></td>
  75. </tr>
  76. </table>
  77. ## Requirements
  78. * Xcode 9.0
  79. * Swift 4.0
  80. * Swift 3.x ([use `rxswift-3.0` branch](https://github.com/ReactiveX/RxSwift/tree/rxswift-3.0) instead)
  81. * Swift 2.3 ([use `rxswift-2.0` branch](https://github.com/ReactiveX/RxSwift/tree/rxswift-2.0) instead)
  82. ## Installation
  83. Rx doesn't contain any external dependencies.
  84. These are currently the supported options:
  85. ### Manual
  86. Open Rx.xcworkspace, choose `RxExample` and hit run. This method will build everything and run the sample app
  87. ### [CocoaPods](https://guides.cocoapods.org/using/using-cocoapods.html)
  88. **Tested with `pod --version`: `1.3.1`**
  89. ```ruby
  90. # Podfile
  91. use_frameworks!
  92. target 'YOUR_TARGET_NAME' do
  93. pod 'RxSwift', '~> 4.0'
  94. pod 'RxCocoa', '~> 4.0'
  95. end
  96. # RxTest and RxBlocking make the most sense in the context of unit/integration tests
  97. target 'YOUR_TESTING_TARGET' do
  98. pod 'RxBlocking', '~> 4.0'
  99. pod 'RxTest', '~> 4.0'
  100. end
  101. ```
  102. Replace `YOUR_TARGET_NAME` and then, in the `Podfile` directory, type:
  103. ```bash
  104. $ pod install
  105. ```
  106. ### [Carthage](https://github.com/Carthage/Carthage)
  107. **Tested with `carthage version`: `0.26.2`**
  108. Add this to `Cartfile`
  109. ```
  110. github "ReactiveX/RxSwift" ~> 4.0
  111. ```
  112. ```bash
  113. $ carthage update
  114. ```
  115. ### [Swift Package Manager](https://github.com/apple/swift-package-manager)
  116. **Tested with `swift build --version`: `Swift 4.0.0-dev (swiftpm-13126)`**
  117. Create a `Package.swift` file.
  118. ```swift
  119. // swift-tools-version:4.0
  120. import PackageDescription
  121. let package = Package(
  122. name: "RxTestProject",
  123. dependencies: [
  124. .package(url: "https://github.com/ReactiveX/RxSwift.git", "4.0.0" ..< "5.0.0")
  125. ],
  126. targets: [
  127. .target(name: "RxTestProject", dependencies: ["RxSwift", "RxCocoa"])
  128. ]
  129. )
  130. ```
  131. ```bash
  132. $ swift build
  133. ```
  134. 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))
  135. ```bash
  136. $ TEST=1 swift test
  137. ```
  138. ### Manually using git submodules
  139. * Add RxSwift as a submodule
  140. ```bash
  141. $ git submodule add git@github.com:ReactiveX/RxSwift.git
  142. ```
  143. * Drag `Rx.xcodeproj` into Project Navigator
  144. * Go to `Project > Targets > Build Phases > Link Binary With Libraries`, click `+` and select `RxSwift-[Platform]` and `RxCocoa-[Platform]` targets
  145. ## References
  146. * [http://reactivex.io/](http://reactivex.io/)
  147. * [Reactive Extensions GitHub (GitHub)](https://github.com/Reactive-Extensions)
  148. * [RxSwift RayWenderlich.com Book](https://store.raywenderlich.com/products/rxswift-reactive-programming-with-swift)
  149. * [Boxue.io RxSwift Online Course](https://boxueio.com/series/rxswift-101) (Chinese 🇨🇳)
  150. * [Erik Meijer (Wikipedia)](http://en.wikipedia.org/wiki/Erik_Meijer_%28computer_scientist%29)
  151. * [Expert to Expert: Brian Beckman and Erik Meijer - Inside the .NET Reactive Framework (Rx) (video)](https://youtu.be/looJcaeboBY)
  152. * [Reactive Programming Overview (Jafar Husain from Netflix)](https://www.youtube.com/watch?v=dwP1TNXE6fc)
  153. * [Subject/Observer is Dual to Iterator (paper)](http://csl.stanford.edu/~christos/pldi2010.fit/meijer.duality.pdf)
  154. * [Rx standard sequence operators visualized (visualization tool)](http://rxmarbles.com/)
  155. * [Haskell](https://www.haskell.org/)