RxAlamofire Documentation

repository·main·Indexed 23 days ago

https://github.com/rxswiftcommunity/rxalamofire

A RxSwift wrapper around Alamofire for reactive networking in Swift. It provides extensions for Alamofire's Session and URLSession to compose network requests, handle responses, and track progress using RxSwift observables.

Tokens
1.6K
Snippets
4
Records
5
Agent score
32%

What's inside RxAlamofire

  1. Install RxAlamofire

    main

    You can install RxAlamofire using CocoaPods, Carthage, Swift Package Manager, or by manual installation.

    ### CocoaPods
    
    Just add to your project's `Podfile`:
    

    pod 'RxAlamofire'

    
    ### Carthage
    
    Add following to `Cartfile`:
    

    gitub "RxSwiftCommunity/RxAlamofire" ~> 6.1

    
    ### Swift Package manager
    
    Create a `Package.swift`  file
    
    ```swift
    // swift-tools-version:5.0
    
    import PackageDescription
    
    let package = Package(
            name: "TestRxAlamofire",
    
            dependencies: [
                .package(url: "https://github.com/RxSwiftCommunity/RxAlamofire.git",
                         from: "6.1.0"),
            ],
    
            targets: [
                .target(
                        name: "TestRxAlamofire",
                        dependencies: ["RxAlamofire"])
            ]
    )

    Manually

    To manual install this extension you should get the RxAlamofire/Source/RxAlamofire.swift imported into your project, alongside RxSwift and Alamofire.

  2. Track request progress with RxAlamofire

    main

    You can track the progress of a request using the .progress() method. This can be used standalone or combined with the request data using Observable.combineLatest to receive both the current progress and the final result.

    // progress
    _ = request(.get, stringURL)
        .progress()
        .observeOn(MainScheduler.instance)
        .subscribe { print($0) }
    
    // progress and final result
    _ = request(.get, stringURL)
        .flatMap { request -> Observable<(Data?, RxProgress)> in
            let dataPart = request.rx
                .data()
                .map { d -> Data? in d }
                .startWith(nil as Data?)
            let progressPart = request.rx.progress()
            return Observable.combineLatest(dataPart, progressPart) { ($0, $1) }
        }
        .observeOn(MainScheduler.instance)
        .subscribe { print($0) }
  3. Use RxAlamofire with Alamofire Session

    main

    RxAlamofire provides extensions to Alamofire's Session object, allowing you to perform reactive network requests with validation, progress tracking, and interceptors. You can use methods like .rx.request(), .rx.json(), .rx.responseString(), and .rx.responseJSON() on a Session instance.

    let session = Session.default
    
    // URLHTTPResponse + Validation + JSON
    _ = session.rx.request(.get, stringURL)
        .validate(statusCode: 200 ..< 300)
        .validate(contentType: ["text/json"])
        .json()
        .observeOn(MainScheduler.instance)
        .subscribe { print($0) }
    
    // URLHTTPResponse + Validation + URLHTTPResponse + String + Progress
    _ = session.rx.request(.get, stringURL)
        .validate(statusCode: 200 ..< 300)
        .validate(contentType: ["text/something"])
        .flatMap { request -> Observable<(String?, RxProgress)> in
            let stringPart = request.rx
                .string()
                .map { d -> String? in d }
                .startWith(nil as String?)
            let progressPart = request.rx.progress()
            return Observable.combineLatest(stringPart, progressPart) { ($0, $1) }
        }
        .observeOn(MainScheduler.instance)
        .subscribe { print($0) }
  4. Use RxAlamofire with URLSession

    main

    RxAlamofire extends URLSession to provide reactive wrappers for standard URLSession tasks, including .rx.response(), .rx.json(), and .rx.data().

    let session = URLSession.shared()
    
    _ = session.rx
        .response(.get, stringURL)
        .observeOn(MainScheduler.instance)
        .subscribe { print($0) }
    
    _ = session.rx
        .json(.get, stringURL)
        .observeOn(MainScheduler.instance)
        .subscribe { print($0) }
  5. RxAlamofire version requirements

    main

    Ensure your environment meets the following requirements:

    • Current Version: Requires Swift 5.1, Alamofire 5.4.1, and RxSwift 6.0.0.
    • Swift 5.0 support: Use RxAlamofire 5.1.0.
    • RxSwift 5.1 support: Use RxAlamofire 5.7.1.
    • Swift 4.2 support: Use RxAlamofire 4.5.0.