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.

254 lines
11 KiB

2 years ago
6 years ago
2 years ago
6 years ago
2 years ago
6 years ago
2 years ago
6 years ago
2 years ago
6 years ago
2 years ago
6 years ago
2 years ago
6 years ago
5 years ago
6 years ago
2 years ago
6 years ago
5 years ago
2 years ago
5 years ago
2 years ago
5 years ago
6 years ago
5 years ago
6 years ago
2 years ago
6 years ago
2 years ago
6 years ago
2 years ago
5 years ago
2 years ago
6 years ago
2 years ago
6 years ago
2 years ago
6 years ago
2 years ago
6 years ago
2 years ago
6 years ago
2 years ago
6 years ago
2 years ago
6 years ago
2 years ago
6 years ago
5 years ago
6 years ago
5 years ago
4 years ago
5 years ago
6 years ago
2 years ago
6 years ago
5 years ago
6 years ago
2 years ago
6 years ago
5 years ago
6 years ago
2 years ago
6 years ago
2 years ago
6 years ago
2 years ago
6 years ago
4 years ago
  1. <p align="center">
  2. <img src="assets/RxSwift_Logo.png" width="35%" alt="RxSwift Logo" />
  3. <br />
  4. <a href="https://actions-badge.atrox.dev/ReactiveX/RxSwift/goto" target="_blank"><img src="https://github.com/ReactiveX/RxSwift/workflows/RxSwift/badge.svg?branch=main" alt="Build Status" /></a>
  5. <img src="https://img.shields.io/badge/platforms-iOS%20%7C%20macOS%20%7C%20tvOS%20%7C%20watchOS%20%7C%20Linux-333333.svg" alt="Supported Platforms: iOS, macOS, tvOS, watchOS & Linux" />
  6. <br />
  7. <a href="https://cocoapods.org/pods/RxSwift" alt="RxSwift on CocoaPods" title="RxSwift on CocoaPods"><img src="https://img.shields.io/cocoapods/v/RxSwift.svg" /></a>
  8. <a href="https://github.com/Carthage/Carthage" alt="RxSwift on Carthage" title="RxSwift on Carthage"><img src="https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat" /></a>
  9. <a href="https://github.com/apple/swift-package-manager" alt="RxSwift on Swift Package Manager" title="RxSwift on Swift Package Manager"><img src="https://img.shields.io/badge/Swift%20Package%20Manager-compatible-brightgreen.svg" /></a>
  10. </p>
  11. Rx is a [generic abstraction of computation](https://youtu.be/looJcaeboBY) expressed through `Observable<Element>` interface, which lets you broadcast and subscribe to values and other events from an `Observable` stream.
  12. RxSwift is the Swift-specific implementation of the [Reactive Extensions](http://reactivex.io) standard.
  13. <p align="center"><img src="assets/example.png" width="55%" alt="RxSwift Observable Example of a price constantly changing and updating the app's UI" /></p>
  14. While this version aims to stay true to the original spirit and naming conventions of Rx, this projects also aims to provide a true Swift-first API for Rx APIs.
  15. Cross platform documentation can be found on [ReactiveX.io](http://reactivex.io/).
  16. Like other Rx implementation, RxSwift's intention is to enable easy composition of asynchronous operations and streams of data in the form of `Observable` objects and a suite of methods to transform and compose these pieces of asynchronous work.
  17. KVO observation, async operations, UI Events and other streams of data 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.
  18. ## I came here because I want to ...
  19. ###### ... understand
  20. * [why use rx?](Documentation/Why.md)
  21. * [the basics, getting started with RxSwift](Documentation/GettingStarted.md)
  22. * [traits](Documentation/Traits.md) - what are `Single`, `Completable`, `Maybe`, `Driver`, and `ControlProperty` ... and why do they exist?
  23. * [testing](Documentation/UnitTests.md)
  24. * [tips and common errors](Documentation/Tips.md)
  25. * [debugging](Documentation/GettingStarted.md#debugging)
  26. * [the math behind Rx](Documentation/MathBehindRx.md)
  27. * [what are hot and cold observable sequences?](Documentation/HotAndColdObservables.md)
  28. ###### ... install
  29. * Integrate RxSwift/RxCocoa with my app. [Installation Guide](#installation)
  30. ###### ... hack around
  31. * with the example app. [Running Example App](Documentation/ExampleApp.md)
  32. * with operators in playgrounds. [Playgrounds](Documentation/Playgrounds.md)
  33. ###### ... interact
  34. * 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)
  35. * Report a problem using the library. [Open an Issue With Bug Template](.github/ISSUE_TEMPLATE.md)
  36. * Request a new feature. [Open an Issue With Feature Request Template](Documentation/NewFeatureRequestTemplate.md)
  37. * Help out [Check out contribution guide](CONTRIBUTING.md)
  38. ###### ... compare
  39. * [with Combine and ReactiveSwift](Documentation/ComparisonWithOtherLibraries.md).
  40. ###### ... understand the structure
  41. RxSwift is as compositional as the asynchronous work it drives. The core unit is RxSwift itself, while other dependencies can be added for UI Work, testing, and more.
  42. It comprises five separate components depending on each other in the following way:
  43. ```none
  44. ┌──────────────┐ ┌──────────────┐
  45. │ RxCocoa ├────▶ RxRelay │
  46. └───────┬──────┘ └──────┬───────┘
  47. │ │
  48. ┌───────▼──────────────────▼───────┐
  49. │ RxSwift │
  50. └───────▲──────────────────▲───────┘
  51. │ │
  52. ┌───────┴──────┐ ┌──────┴───────┐
  53. │ RxTest │ │ RxBlocking │
  54. └──────────────┘ └──────────────┘
  55. ```
  56. * **RxSwift**: The core of RxSwift, providing the Rx standard as (mostly) defined by [ReactiveX](https://reactivex.io). It has no other dependencies.
  57. * **RxCocoa**: Provides Cocoa-specific capabilities for general iOS/macOS/watchOS & tvOS app development, such as Shared Sequences, Traits, and much more. It depends on both `RxSwift` and `RxRelay`.
  58. * **RxRelay**: Provides `PublishRelay`, `BehaviorRelay` and `ReplayRelay`, three [simple wrappers around Subjects](https://github.com/ReactiveX/RxSwift/blob/main/Documentation/Subjects.md#relays). It depends on `RxSwift`.
  59. * **RxTest** and **RxBlocking**: Provides testing capabilities for Rx-based systems. It depends on `RxSwift`.
  60. ## Usage
  61. <table>
  62. <tr>
  63. <th width="30%">Here's an example</th>
  64. <th width="30%">In Action</th>
  65. </tr>
  66. <tr>
  67. <td>Define search for GitHub repositories ...</td>
  68. <th rowspan="9"><img src="https://raw.githubusercontent.com/kzaher/rxswiftcontent/master/GithubSearch.gif"></th>
  69. </tr>
  70. <tr>
  71. <td><div class="highlight highlight-source-swift"><pre>
  72. let searchResults = searchBar.rx.text.orEmpty
  73. .throttle(.milliseconds(300), scheduler: MainScheduler.instance)
  74. .distinctUntilChanged()
  75. .flatMapLatest { query -> Observable&lt;[Repository]&gt; in
  76. if query.isEmpty {
  77. return .just([])
  78. }
  79. return searchGitHub(query)
  80. .catchAndReturn([])
  81. }
  82. .observe(on: MainScheduler.instance)</pre></div></td>
  83. </tr>
  84. <tr>
  85. <td>... then bind the results to your tableview</td>
  86. </tr>
  87. <tr>
  88. <td width="30%"><div class="highlight highlight-source-swift"><pre>
  89. searchResults
  90. .bind(to: tableView.rx.items(cellIdentifier: "Cell")) {
  91. (index, repository: Repository, cell) in
  92. cell.textLabel?.text = repository.name
  93. cell.detailTextLabel?.text = repository.url
  94. }
  95. .disposed(by: disposeBag)</pre></div></td>
  96. </tr>
  97. </table>
  98. ## Requirements
  99. * Xcode 12.x
  100. * Swift 5.x
  101. For Xcode 11 and below, [use RxSwift 5.x](https://github.com/ReactiveX/RxSwift/releases/tag/5.1.1).
  102. ## Installation
  103. RxSwift doesn't contain any external dependencies.
  104. These are currently the supported installation options:
  105. ### Manual
  106. Open Rx.xcworkspace, choose `RxExample` and hit run. This method will build everything and run the sample app
  107. ### [CocoaPods](https://guides.cocoapods.org/using/using-cocoapods.html)
  108. ```ruby
  109. # Podfile
  110. use_frameworks!
  111. target 'YOUR_TARGET_NAME' do
  112. pod 'RxSwift', '6.5.0'
  113. pod 'RxCocoa', '6.5.0'
  114. end
  115. # RxTest and RxBlocking make the most sense in the context of unit/integration tests
  116. target 'YOUR_TESTING_TARGET' do
  117. pod 'RxBlocking', '6.5.0'
  118. pod 'RxTest', '6.5.0'
  119. end
  120. ```
  121. Replace `YOUR_TARGET_NAME` and then, in the `Podfile` directory, type:
  122. ```bash
  123. $ pod install
  124. ```
  125. ### XCFrameworks
  126. Each release starting with RxSwift 6 includes `*.xcframework` framework binaries.
  127. Simply drag the needed framework binaries to your **Frameworks, Libraries, and Embedded Content** section under your target's **General** tab.
  128. > **Note**: If you're using `RxCocoa`, be sure to also drag **RxCocoaRuntime.xcframework** before importing `RxCocoa`.
  129. <img src="https://raw.githubusercontent.com/ReactiveX/RxSwift/main/assets/xcframeworks.png" alt="XCFrameworks instructions" width="65%">
  130. ### [Carthage](https://github.com/Carthage/Carthage)
  131. Add this to `Cartfile`
  132. ```
  133. github "ReactiveX/RxSwift" "6.5.0"
  134. ```
  135. ```bash
  136. $ carthage update
  137. ```
  138. #### Carthage as a Static Library
  139. Carthage defaults to building RxSwift as a Dynamic Library.
  140. 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:
  141. ```bash
  142. carthage update RxSwift --platform iOS --no-build
  143. sed -i -e 's/MACH_O_TYPE = mh_dylib/MACH_O_TYPE = staticlib/g' Carthage/Checkouts/RxSwift/Rx.xcodeproj/project.pbxproj
  144. carthage build RxSwift --platform iOS
  145. ```
  146. ### [Swift Package Manager](https://github.com/apple/swift-package-manager)
  147. > **Note**: There is a critical cross-dependency bug affecting many projects including RxSwift in Swift Package Manager. We've [filed a bug (SR-12303)](https://bugs.swift.org/browse/SR-12303) in early 2020 but have no answer yet. Your mileage may vary. A partial workaround can be found [here](https://github.com/ReactiveX/RxSwift/issues/2127#issuecomment-717830502).
  148. Create a `Package.swift` file.
  149. ```swift
  150. // swift-tools-version:5.0
  151. import PackageDescription
  152. let package = Package(
  153. name: "RxTestProject",
  154. dependencies: [
  155. .package(url: "https://github.com/ReactiveX/RxSwift.git", .exact("6.5.0"))
  156. ],
  157. targets: [
  158. .target(name: "RxTestProject", dependencies: ["RxSwift", "RxCocoa"])
  159. ]
  160. )
  161. ```
  162. ```bash
  163. $ swift build
  164. ```
  165. To build or test a module with RxTest dependency, set `TEST=1`.
  166. ```bash
  167. $ TEST=1 swift test
  168. ```
  169. ### Manually using git submodules
  170. * Add RxSwift as a submodule
  171. ```bash
  172. $ git submodule add git@github.com:ReactiveX/RxSwift.git
  173. ```
  174. * Drag `Rx.xcodeproj` into Project Navigator
  175. * Go to `Project > Targets > Build Phases > Link Binary With Libraries`, click `+` and select `RxSwift`, `RxCocoa` and `RxRelay` targets
  176. ## References
  177. * [http://reactivex.io/](http://reactivex.io/)
  178. * [Reactive Extensions GitHub (GitHub)](https://github.com/Reactive-Extensions)
  179. * [RxSwift RayWenderlich.com Book](https://store.raywenderlich.com/products/rxswift-reactive-programming-with-swift)
  180. * [RxSwift: Debunking the myth of hard (YouTube)](https://www.youtube.com/watch?v=GdvLP0ZAhhc)
  181. * [Boxue.io RxSwift Online Course](https://boxueio.com/series/rxswift-101) (Chinese 🇨🇳)
  182. * [Expert to Expert: Brian Beckman and Erik Meijer - Inside the .NET Reactive Framework (Rx) (video)](https://youtu.be/looJcaeboBY)
  183. * [Reactive Programming Overview (Jafar Husain from Netflix)](https://youtu.be/-8Y1-lE6NSA)
  184. * [Subject/Observer is Dual to Iterator (paper)](http://csl.stanford.edu/~christos/pldi2010.fit/meijer.duality.pdf)
  185. * [Rx standard sequence operators visualized (visualization tool)](http://rxmarbles.com/)
  186. * [Haskell](https://www.haskell.org/)