1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
import RxSwift
import RxCocoa
final class MainModel {
private let subModel = SubModel()
init() {
}
func turnOn() {
self.subModel.turnOn()
}
}
final class SubModel {
private var logic1Subject: PublishSubject<Bool> = PublishSubject<Bool>()
private var logic2Subject: PublishSubject<Bool> = PublishSubject<Bool>()
private let disposeBag = DisposeBag()
init() {
self.hoge()
}
func turnOn() {
self.logic1Subject.onNext(true)
self.logic2Subject.onNext(true)
}
func turnOff() {
self.logic1Subject.onNext(false)
}
private func hoge() {
return Observable
.of(
logic1Subject,
logic2Subject
)
.merge()
self.logic1Subject.subscribe { (event) in
print("hoge1")
}.disposed(by: self.disposeBag)
self.logic2Subject.subscribe { (event) in
print("hoge2")
}.disposed(by: self.disposeBag)
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
import RxSwift
import RxCocoa
final class TaskManager {
static let shared: TaskManager = TaskManager()
let isActive: PublishSubject<Bool> = PublishSubject<Bool>()
func isReady() -> Observable<Bool> {
return Observable<Bool>.create({ (observer) -> Disposable in
observer.onNext(self.isActive) // NG
observer.onCompleted()
return Disposables.create()
})
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import UIKit
import RxSwift
import RxCocoa
final class TaskListViewController: UIViewController {
override func viewDidLoad() {
TaskManager.shared.isReady().subscribe { (event) in
print("hoge1")
}.disposed(by: self.disposeBag)
TaskManager.shared.isActive.onNext(false)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
TaskManager.shared.isActive.onNext(true)
}
}
|
TaskManager.shared.isActive.value = false
をしても、 TaskManager.shared.isReady().subscribe
は反応しない。