如何初始化RxSwift中的可观察属性?

时间:2022-09-25 12:36:31

I find this very puzzling. Coming from ReactiveCocoa I would expect something like this possible.

我觉得这很令人费解。来自ReactiveCocoa我希望这样的事情可能。

如何初始化RxSwift中的可观察属性?

How can I initialize the RxSwift observable to 5?

如何将RxSwift observable初始化为5?

5 个解决方案

#1


1  

I am not able to test it right now, but wouldn't Observable.just be the function you are looking for?

我现在无法测试它,但不会Observable.just是你正在寻找的功能?

Source for Observable creation: github link

Observable创建的来源:github链接

Of course, you could also use a Variable(5) if you intend to modify it.

当然,如果您打算修改它,也可以使用变量(5)。

#2


1  

As I am learning RxSwift I ran up on this thread. You can initialize an observable property like this:

当我学习RxSwift时,我跑上了这个帖子。您可以初始化一个可观察的属性,如下所示:

var age = Variable<Int>(5)

And set it up as an observable:

并将其设置为可观察的:

let disposeBag = DisposeBag()

private func setupAgeObserver() {
    age.asObservable()
      .subscribe(onNext: {
        years in
        print ("age is \(years)")
        // do something
      })
      .addDisposableTo(disposeBag)
 }

#3


0  

in Rx in general, there is a BehaviorSubject which stores the last value

在Rx中,通常有一个BehaviorSubject存储最后一个值

specifically in RxSwift, there is also Variable which is a wrapper around BehaviorSubject

特别是在RxSwift中,还有Variable,它是BehaviorSubject的包装器

see description of both here - https://github.com/ReactiveX/RxSwift/blob/master/Documentation/GettingStarted.md

在这里看到两者的描述 - https://github.com/ReactiveX/RxSwift/blob/master/Documentation/GettingStarted.md

#4


0  

In RxSwift Variable is deprecated. Use BehaviorRelay or BehaviorSubject

在RxSwift中,不推荐使用变量。使用BehaviorRelay或BehaviorSubject

#5


0  

You can create stream in multiple ways:

您可以通过多种方式创建流:

Main way

主要方式

    Observable<Int>.create { observer -> Disposable in

        // Send events through observer
        observer.onNext(3)
        observer.onError(NSError(domain: "", code: 1, userInfo: nil))
        observer.onCompleted()

        return Disposables.create {
            // Clean up when stream is deallocated
        }
    }

Shortcuts

快捷键

    Observable<Int>.empty() // Completes straight away
    Observable<Int>.never() // Never gets any event into the stream, waits forever
    Observable<Int>.just(1) // Emit value "1" and completes

Through Subjects (aka Property / MutableProperty in ReactiveSwift)

通过主题(ReactiveSwift中的属性/ MutableProperty)

Variable is deprecated in RxSwift 4, but it's just a wrapper around BehaviourSubject, so you can use it instead.

变量在RxSwift 4中已弃用,但它只是BehaviourSubject的一个包装器,因此您可以使用它。

There are 2 most used subjects

有2个最常用的科目

BehaviorSubject - it will emit current value and upcoming ones. Because it will emit current value it needs to be initialised with a value BehaviorSubject<Int>(value: 0)

BehaviorSubject - 它将发出当前值和即将发布的值。因为它将发出当前值,所以需要使用值BehaviorSubject (值:0)初始化它

PublishSubject - it will emit upcoming values. It doesn't require initial value while initialising PublishSubject<Int>()

PublishSubject - 它将发出即将到来的值。初始化PublishSubject ()时不需要初始值

Then you can call .asObservable() on subject instance to get an observable.

然后你可以在主题实例上调用.asObservable()来获得一个observable。

#1


1  

I am not able to test it right now, but wouldn't Observable.just be the function you are looking for?

我现在无法测试它,但不会Observable.just是你正在寻找的功能?

Source for Observable creation: github link

Observable创建的来源:github链接

Of course, you could also use a Variable(5) if you intend to modify it.

当然,如果您打算修改它,也可以使用变量(5)。

#2


1  

As I am learning RxSwift I ran up on this thread. You can initialize an observable property like this:

当我学习RxSwift时,我跑上了这个帖子。您可以初始化一个可观察的属性,如下所示:

var age = Variable<Int>(5)

And set it up as an observable:

并将其设置为可观察的:

let disposeBag = DisposeBag()

private func setupAgeObserver() {
    age.asObservable()
      .subscribe(onNext: {
        years in
        print ("age is \(years)")
        // do something
      })
      .addDisposableTo(disposeBag)
 }

#3


0  

in Rx in general, there is a BehaviorSubject which stores the last value

在Rx中,通常有一个BehaviorSubject存储最后一个值

specifically in RxSwift, there is also Variable which is a wrapper around BehaviorSubject

特别是在RxSwift中,还有Variable,它是BehaviorSubject的包装器

see description of both here - https://github.com/ReactiveX/RxSwift/blob/master/Documentation/GettingStarted.md

在这里看到两者的描述 - https://github.com/ReactiveX/RxSwift/blob/master/Documentation/GettingStarted.md

#4


0  

In RxSwift Variable is deprecated. Use BehaviorRelay or BehaviorSubject

在RxSwift中,不推荐使用变量。使用BehaviorRelay或BehaviorSubject

#5


0  

You can create stream in multiple ways:

您可以通过多种方式创建流:

Main way

主要方式

    Observable<Int>.create { observer -> Disposable in

        // Send events through observer
        observer.onNext(3)
        observer.onError(NSError(domain: "", code: 1, userInfo: nil))
        observer.onCompleted()

        return Disposables.create {
            // Clean up when stream is deallocated
        }
    }

Shortcuts

快捷键

    Observable<Int>.empty() // Completes straight away
    Observable<Int>.never() // Never gets any event into the stream, waits forever
    Observable<Int>.just(1) // Emit value "1" and completes

Through Subjects (aka Property / MutableProperty in ReactiveSwift)

通过主题(ReactiveSwift中的属性/ MutableProperty)

Variable is deprecated in RxSwift 4, but it's just a wrapper around BehaviourSubject, so you can use it instead.

变量在RxSwift 4中已弃用,但它只是BehaviourSubject的一个包装器,因此您可以使用它。

There are 2 most used subjects

有2个最常用的科目

BehaviorSubject - it will emit current value and upcoming ones. Because it will emit current value it needs to be initialised with a value BehaviorSubject<Int>(value: 0)

BehaviorSubject - 它将发出当前值和即将发布的值。因为它将发出当前值,所以需要使用值BehaviorSubject (值:0)初始化它

PublishSubject - it will emit upcoming values. It doesn't require initial value while initialising PublishSubject<Int>()

PublishSubject - 它将发出即将到来的值。初始化PublishSubject ()时不需要初始值

Then you can call .asObservable() on subject instance to get an observable.

然后你可以在主题实例上调用.asObservable()来获得一个observable。