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.

243 lines
9.6 KiB

6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 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. Rx is a [generic abstraction of computation](https://youtu.be/looJcaeboBY) expressed through `Observable<Element>` interface.
  5. This is a Swift version of [Rx](https://github.com/Reactive-Extensions/Rx.NET).
  6. 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.
  7. Cross platform documentation can be found on [ReactiveX.io](http://reactivex.io/).
  8. Like the original Rx, its intention is to enable easy composition of asynchronous operations and event/data streams.
  9. 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.
  10. ## I came here because I want to ...
  11. ###### ... understand
  12. * [why use rx?](Documentation/Why.md)
  13. * [the basics, getting started with RxSwift](Documentation/GettingStarted.md)
  14. * [traits](Documentation/Traits.md) - what are `Single`, `Completable`, `Maybe`, `Driver`, and `ControlProperty` ... and why do they exist?
  15. * [testing](Documentation/UnitTests.md)
  16. * [tips and common errors](Documentation/Tips.md)
  17. * [debugging](Documentation/GettingStarted.md#debugging)
  18. * [the math behind Rx](Documentation/MathBehindRx.md)
  19. * [what are hot and cold observable sequences?](Documentation/HotAndColdObservables.md)
  20. ###### ... install
  21. * Integrate RxSwift/RxCocoa with my app. [Installation Guide](#installation)
  22. ###### ... hack around
  23. * with the example app. [Running Example App](Documentation/ExampleApp.md)
  24. * with operators in playgrounds. [Playgrounds](Documentation/Playgrounds.md)
  25. ###### ... interact
  26. * All of this is great, but it would be nice to talk with other people using RxSwift and exchange experiences. <br />[Join Slack Channel](http://slack.rxswift.org)
  27. * Report a problem using the library. [Open an Issue With Bug Template](.github/ISSUE_TEMPLATE.md)
  28. * Request a new feature. [Open an Issue With Feature Request Template](Documentation/NewFeatureRequestTemplate.md)
  29. * Help out [Check out contribution guide](CONTRIBUTING.md)
  30. ###### ... compare
  31. * [with other libraries](Documentation/ComparisonWithOtherLibraries.md).
  32. ###### ... understand the structure
  33. RxSwift comprises five separate components depending on eachother in the following way:
  34. ```none
  35. ┌──────────────┐ ┌──────────────┐
  36. │ RxCocoa ├────▶ RxRelay │
  37. └───────┬──────┘ └──────┬───────┘
  38. │ │
  39. ┌───────▼──────────────────▼───────┐
  40. │ RxSwift │
  41. └───────▲──────────────────▲───────┘
  42. │ │
  43. ┌───────┴──────┐ ┌──────┴───────┐
  44. │ RxTest │ │ RxBlocking │
  45. └──────────────┘ └──────────────┘
  46. ```
  47. * **RxSwift**: The core of RxSwift, providing the Rx standard as (mostly) defined by [ReactiveX](https://reactivex.io). It has no other dependencies.
  48. * **RxCocoa**: Provides Cocoa-specific capabilities for general iOS/macOS/watchOS & tvOS app development, such as Binders, Traits, and much more. It depends on both `RxSwift` and `RxRelay`.
  49. * **RxRelay**: Provides `PublishRelay` and `BehaviorRelay`, two [simple wrappers around Subjects](https://github.com/ReactiveX/RxSwift/blob/master/Documentation/Subjects.md#relays). It depends on `RxSwift`.
  50. * **RxTest** and **RxBlocking**: Provides testing capabilities for Rx-based systems. It depends on `RxSwift`.
  51. ###### ... find compatible
  52. * libraries from [RxSwiftCommunity](https://github.com/RxSwiftCommunity).
  53. * [Pods using RxSwift](https://cocoapods.org/?q=uses%3Arxswift).
  54. ###### ... see the broader vision
  55. * Does this exist for Android? [RxJava](https://github.com/ReactiveX/RxJava)
  56. * 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.
  57. ## Usage
  58. <table>
  59. <tr>
  60. <th width="30%">Here's an example</th>
  61. <th width="30%">In Action</th>
  62. </tr>
  63. <tr>
  64. <td>Define search for GitHub repositories ...</td>
  65. <th rowspan="9"><img src="https://raw.githubusercontent.com/kzaher/rxswiftcontent/master/GithubSearch.gif"></th>
  66. </tr>
  67. <tr>
  68. <td><div class="highlight highlight-source-swift"><pre>
  69. let searchResults = searchBar.rx.text.orEmpty
  70. .throttle(.milliseconds(300), scheduler: MainScheduler.instance)
  71. .distinctUntilChanged()
  72. .flatMapLatest { query -> Observable&lt;[Repository]&gt; in
  73. if query.isEmpty {
  74. return .just([])
  75. }
  76. return searchGitHub(query)
  77. .catchErrorJustReturn([])
  78. }
  79. .observeOn(MainScheduler.instance)</pre></div></td>
  80. </tr>
  81. <tr>
  82. <td>... then bind the results to your tableview</td>
  83. </tr>
  84. <tr>
  85. <td width="30%"><div class="highlight highlight-source-swift"><pre>
  86. searchResults
  87. .bind(to: tableView.rx.items(cellIdentifier: "Cell")) {
  88. (index, repository: Repository, cell) in
  89. cell.textLabel?.text = repository.name
  90. cell.detailTextLabel?.text = repository.url
  91. }
  92. .disposed(by: disposeBag)</pre></div></td>
  93. </tr>
  94. </table>
  95. ## Requirements
  96. * Xcode 10.2
  97. * Swift 5.0
  98. For Xcode 10.1 and below, [use RxSwift 4.5](https://github.com/ReactiveX/RxSwift/releases/tag/4.5.0).
  99. ## Installation
  100. Rx doesn't contain any external dependencies.
  101. These are currently the supported options:
  102. ### Manual
  103. Open Rx.xcworkspace, choose `RxExample` and hit run. This method will build everything and run the sample app
  104. ### [CocoaPods](https://guides.cocoapods.org/using/using-cocoapods.html)
  105. ```ruby
  106. # Podfile
  107. use_frameworks!
  108. target 'YOUR_TARGET_NAME' do
  109. pod 'RxSwift', '~> 5'
  110. pod 'RxCocoa', '~> 5'
  111. end
  112. # RxTest and RxBlocking make the most sense in the context of unit/integration tests
  113. target 'YOUR_TESTING_TARGET' do
  114. pod 'RxBlocking', '~> 5'
  115. pod 'RxTest', '~> 5'
  116. end
  117. ```
  118. Replace `YOUR_TARGET_NAME` and then, in the `Podfile` directory, type:
  119. ```bash
  120. $ pod install
  121. ```
  122. ### [Carthage](https://github.com/Carthage/Carthage)
  123. Officially supported: Carthage 0.33 and up.
  124. Add this to `Cartfile`
  125. ```
  126. github "ReactiveX/RxSwift" ~> 5.0
  127. ```
  128. ```bash
  129. $ carthage update
  130. ```
  131. #### Carthage as a Static Library
  132. Carthage defaults to building RxSwift as a Dynamic Library.
  133. If you wish to build RxSwift as a Static Library using Carthage you may use the script below to manually modify the framework type before building with Carthage:
  134. ```bash
  135. carthage update RxSwift --platform iOS --no-build
  136. sed -i -e 's/MACH_O_TYPE = mh_dylib/MACH_O_TYPE = staticlib/g' Carthage/Checkouts/RxSwift/Rx.xcodeproj/project.pbxproj
  137. carthage build RxAlamofire --platform iOS
  138. ```
  139. ### [Swift Package Manager](https://github.com/apple/swift-package-manager)
  140. Create a `Package.swift` file.
  141. ```swift
  142. // swift-tools-version:5.0
  143. import PackageDescription
  144. let package = Package(
  145. name: "RxTestProject",
  146. dependencies: [
  147. .package(url: "https://github.com/ReactiveX/RxSwift.git", from: "5.0.0")
  148. ],
  149. targets: [
  150. .target(name: "RxTestProject", dependencies: ["RxSwift", "RxCocoa"])
  151. ]
  152. )
  153. ```
  154. ```bash
  155. $ swift build
  156. ```
  157. To build or test a module with RxTest dependency, set `TEST=1`.
  158. ```bash
  159. $ TEST=1 swift test
  160. ```
  161. ### Manually using git submodules
  162. * Add RxSwift as a submodule
  163. ```bash
  164. $ git submodule add git@github.com:ReactiveX/RxSwift.git
  165. ```
  166. * Drag `Rx.xcodeproj` into Project Navigator
  167. * Go to `Project > Targets > Build Phases > Link Binary With Libraries`, click `+` and select `RxSwift-[Platform]` and `RxCocoa-[Platform]` targets
  168. ## References
  169. * [http://reactivex.io/](http://reactivex.io/)
  170. * [Reactive Extensions GitHub (GitHub)](https://github.com/Reactive-Extensions)
  171. * [RxSwift RayWenderlich.com Book](https://store.raywenderlich.com/products/rxswift-reactive-programming-with-swift)
  172. * [Boxue.io RxSwift Online Course](https://boxueio.com/series/rxswift-101) (Chinese 🇨🇳)
  173. * [Erik Meijer (Wikipedia)](http://en.wikipedia.org/wiki/Erik_Meijer_%28computer_scientist%29)
  174. * [Expert to Expert: Brian Beckman and Erik Meijer - Inside the .NET Reactive Framework (Rx) (video)](https://youtu.be/looJcaeboBY)
  175. * [Reactive Programming Overview (Jafar Husain from Netflix)](https://www.youtube.com/watch?v=dwP1TNXE6fc)
  176. * [Subject/Observer is Dual to Iterator (paper)](http://csl.stanford.edu/~christos/pldi2010.fit/meijer.duality.pdf)
  177. * [Rx standard sequence operators visualized (visualization tool)](http://rxmarbles.com/)
  178. * [Haskell](https://www.haskell.org/)