Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ashfurrow/reactivemoya
https://github.com/ashfurrow/reactivemoya
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/ashfurrow/reactivemoya
- Owner: ashfurrow
- License: mit
- Created: 2015-08-09T15:15:12.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-09-07T16:43:09.000Z (over 9 years ago)
- Last Synced: 2024-05-09T20:02:30.446Z (8 months ago)
- Language: Swift
- Size: 7.37 MB
- Stars: 22
- Watchers: 8
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ReactiveMoya
A simpler way to use Moya+ReactiveCocoa.Installation
------------------------
####Carthage
Add this line to your `Cartfile`:
```
github "Moya/ReactiveMoya"
```
and run `carthage update`Usage
------------------------ReactiveMoya is the Moya you know, with the RAC interfaces you're used to. `ReactiveMoyaProvider` immediately returns a `SignalProducer` or `RACSignal` that you can subscribe to or bind or map or whatever you want to
do. To handle errors, for instance, we could do the following:```swift
provider.request(.UserProfile("ashfurrow"))
.subscribeNext({ (object) -> Void in
if let response = object as? MoyaResponse {
image = UIImage(data: response.data)
}
}, error: { (error) -> Void in
println(error)
})
``````swift
provider.request(.UserProfile("ashfurrow"))
|> start(error: { error in
println(error)
},
next: { (object: MoyaResponse) in
image = UIImage(data: object.data)
})
```In addition to the option of using signals instead of callback blocks, there are
also a series of signal operators that will attempt to map the data received
from the network response into either an image, some JSON, or a string, with
`mapImage()`, `mapJSON()`, `mapJSONArray()`, `mapJSONDictionary()`, and `mapString()`, respectively.
If the mapping is unsuccessful, you'll get an error on the producer or signal. You also get handy methods for
filtering out certain status codes. This means that you can place your code for
handling API errors like 400's in the same places as code for handling invalid
responses.The example above becomes:
```swift
provider.request(.UserProfile("ashfurrow"))
.mapImage()
.subscribeNext({ (object: AnyObject) in
if let image = object as? UIImage {
// Do something with the image
}
},
error: (error: NSError) in
println(error)
})
``````swift
provider.request(.UserProfile("ashfurrow"))
|> mapImage()
|> start(error: { (error: NSError) in
println(error)
},
next: { (image: UIImage) in
// Do something with the image
})
```Running the Example
------------------------
To run the example:####Using Carthage
Navigate to the root of the directory and run `carthage bootstrap`
Open the Example project and build