如何将 Observable 转换为 RxSwift 中的 Single

huangapple go评论56阅读模式
英文:

How to convert Observable<Type> to Single<Type> in RxSwift?

问题

I currently have a method that returns an observable. However, it only ever returns one element.

func getMyResponse(queryID: String) -&gt; Observable&lt;Response&gt; {
    return observable
}

I am trying to refactor this code from returning Observable&lt;Response&gt; to Single&lt;Response&gt;.

func getMyResponse(queryID: String) -&gt; Single&lt;Response&gt; {
    return observable.first()
}

The above is not working and gives the following error:

Cannot convert return expression of type &#39;PrimitiveSequence&lt;SingleTrait, Response?&gt;&#39; to return type &#39;Single&lt;Response&gt;`.

Anyone know how to fix this issue? Any insight would be much appreciated!

英文:

I currently have a method that returns an observable. However, it only ever returns one element.

func getMyResponse(queryID: String) -&gt; Observable&lt;Response&gt; {
    return observable
}

I am trying to refactor this code from returning Observable&lt;Response&gt; to Single&lt;Response&gt;.

func getMyResponse(queryID: String) -&gt; Single&lt;Response&gt; {
    return observable.first()
}

The above is not working and gives the following error:

Cannot convert return expression of type &#39;PrimitiveSequence&lt;SingleTrait, Response?&gt;&#39; to return type &#39;Single&lt;Response&gt;`.

Anyone know how to fix this issue? Any insight would be much appreciated!

答案1

得分: 1

You are telling the compiler that you will return a Single&lt;Response&gt; from the function, but you are actually returning a Single&lt;Response?&gt;. That's an error.

Note that Single&lt;Response?&gt; is a typealias of PrimitiveSequence&lt;SingleTrait, Response?&gt;.

For more information. You could do either of the following:

func option1(queryID: String) -&gt; Single&lt;Response&gt; {
    return observable.asSingle()
}

func option2(queryID: String) -&gt; Single&lt;Response?&gt; {
    return observable.first()
}

For `option1`... If the observable completes without emitting a value, the Single will emit an error `RxError.noElements`. If the observable emits a second value before completing, the `asSingle()` will convert that second value into an error `RxError.moreThanOneElement`.

For `option2`... If the observable completes without emitting a value, it will emit `nil` and complete. And the observable will be disposed before it gets a chance to emit a second value.

<details>
<summary>英文:</summary>

You are telling the compiler that you will return a `Single&lt;Response&gt;` from the function, but you are actually returning a `Single&lt;Response?&gt;`. That&#39;s an error.

Note that `Single&lt;Response?&gt;` is a typealias of `PrimitiveSequence&lt;SingleTrait, Response?&gt;`.

For more information. You could do either of the following:

	func option1(queryID: String) -&gt; Single&lt;Response&gt; {
		return observable.asSingle()
	}

	func option2(queryID: String) -&gt; Single&lt;Response?&gt; {
		return observable.first()
	}

For `option1`... If the observable completes without emitting a value the Single will emit an error `RxError.noElements`. If the observable emits a second value before completing, the `asSingle()` will convert that second value into an error `RxError.moreThanOneElement`

For `option2`... If the observable completes without emitting a value, it will emit `nil` and complete. And the observable will be disposed before it gets a chance to emit a second value.

</details>



huangapple
  • 本文由 发表于 2023年3月31日 04:12:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892604.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定