ReactiveX / RxSwift

Reactive Programming in Swift
MIT License
24.32k stars 4.17k forks source link

asSingle -> Sequence contains more than one element. #2579

Closed EE-Yat-Ho closed 7 months ago

EE-Yat-Ho commented 7 months ago

Short description of the issue:

asSingle does not forwardOn the value entered as next. When Subject or Observable is changed to asSingle, it does not work properly.

Expected outcome:

It works normally when you change Subject or Observable to asSingle.

Self contained code example that reproduces the issue:

let ps = PublishSubject()
let disposeBag = DisposeBag()

ps.asSingle()
    .subscribe(onSuccess: { string in
        print("❔ onSuccess \(string)")
    }, onFailure: { error in
        print("❔ onFailure \(error)")
    }, onDisposed: {
        print("❔ onDisposed")
    })
    .disposed(by: disposeBag)

ps.onNext("a")
// not thing print
ps.onNext("b")
// print "❔ onFailure Sequence contains more than one element."
// print "❔ onDisposed"

Platform/Environment

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

Xcode version:

15.1

:warning: Fields below are optional for general issues or in case those questions aren't related to your issue, but filling them out will increase the chances of getting your issue resolved. :warning:

Installation method:

I have multiple versions of Xcode installed: (so we can know if this is a potential cause of your issue)

Level of RxSwift knowledge: (this is so we can understand your level of knowledge and formulate the response in an appropriate manner)

EE-Yat-Ho commented 7 months ago

I would like to add the code below to .next of AsSingleSink.

self.forwardOn(element)
self.forwardOn(.completed)
danielt1263 commented 7 months ago

That is as designed. In your case, use ps.take(1).asSingle()

EE-Yat-Ho commented 7 months ago

“completed” is issued when “take” receives the specified number of events. Great.

ghost commented 7 months ago

Short description of the issue:

asSingle does not forwardOn the value entered as next.

When Subject or Observable is changed to asSingle, it does not work properly.

Expected outcome:

It works normally when you change Subject or Observable to asSingle.

Self contained code example that reproduces the issue:


let ps = PublishSubject()

let disposeBag = DisposeBag()

ps.asSingle()

    .subscribe(onSuccess: { string in

        print("❔ onSuccess \(string)")

    }, onFailure: { error in

        print("❔ onFailure \(error)")

    }, onDisposed: {

        print("❔ onDisposed")

    })

    .disposed(by: disposeBag)

ps.onNext("a")

// not thing print

ps.onNext("b")

// print "❔ onFailure Sequence contains more than one element."

// print "❔ onDisposed"

Platform/Environment

  • [x] iOS

  • [ ] 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:


15.1

:warning: Fields below are optional for general issues or in case those questions aren't related to your issue, but filling them out will increase the chances of getting your issue resolved. :warning:

Installation method:

  • [ ] CocoaPods

  • [ ] Carthage

  • [x] Git submodules

I have multiple versions of Xcode installed:

(so we can know if this is a potential cause of your issue)

  • [x] yes (which ones)

  • [ ] no

Level of RxSwift knowledge:

(this is so we can understand your level of knowledge

and formulate the response in an appropriate manner)
  • [ ] just starting

  • [x] I have a small code base

  • [ ] I have a significant code base

danielt1263 commented 7 months ago

@Efebyts I'm not sure why you quoted the original message to this closed topic... Care to elaborate?