Rxswift: Cant use .reduce from Variable.asObservable()

Created on 22 Feb 2018  路  1Comment  路  Source: ReactiveX/RxSwift

Short description of the issue:

When trying to use the reduce method from Variable.asObservable() it doesn't seem to call the property or method in the subscribe method

Expected outcome:

To be consistent with Observable object and return the value on subscribe with it calling the method and property

What actually happens:

it doesn't seem to call the property or method in the subscribe method.

Self contained code example that reproduces the issue:

import XCTest
@testable import RxSwift

class VariableReduceTest: XCTestCase {

    let dispose = DisposeBag()

    func testVariableCanReduce() {
        let expectation = XCTestExpectation(description: "Reduce from Variable")

        var myText: String? {
            didSet {
                print("Set: \(myText)")
            }
        }

        let variable = Variable(0)
        let observable = variable.asObservable()

        DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: { variable.value = 1 })
        DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: { variable.value = 2 })

        observable
            .debug()
            .reduce("", accumulator: { (text, int) in
                 return "ABC"
            })
            .subscribe(onNext: { newValue in
                print("Value: \(newValue)")

                // it assign value here
                myText = newValue

                // all pass here
                XCTAssertNotNil(myText)
                XCTAssertTrue(myText?.isEmpty == false)
                XCTAssertTrue(myText == "ABC")

                expectation.fulfill()
            })
            .disposed(by: dispose)

        // myText fails all test here
        XCTAssertNotNil(myText)
        XCTAssertTrue(myText?.isEmpty == false)
        XCTAssertTrue(myText == "ABC")

        // filfill doesn't get called here
        wait(for: [expectation], timeout: 5.0)
    }
}

RxSwift/RxCocoa/RxBlocking/RxTest version/commit
4.1.2 and also used master

Platform/Environment

  • [x] iOS
  • [x] macOS
  • [ ] tvOS
  • [ ] watchOS
  • [ ] playgrounds

How easy is to reproduce? (chances of successful reproduce after running the self contained code)

  • [x] easy, 100% repro
  • [ ] sometimes, 10%-100%
  • [ ] hard, 2% - 10%
  • [ ] extremely hard, %0 - 2%

Xcode version:

 9.2

Most helpful comment

Hi @shams-ahmed ,

reduce emits only one element when sequence completes. You probably want scan operator.

>All comments

Hi @shams-ahmed ,

reduce emits only one element when sequence completes. You probably want scan operator.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

apoloa picture apoloa  路  3Comments

marlowcharite picture marlowcharite  路  3Comments

Z-JaDe picture Z-JaDe  路  3Comments

hannesstruss picture hannesstruss  路  3Comments

trungp picture trungp  路  3Comments