RxSwift 雑記

Monday, October 8, 2018

RxSwiftのメモ。

Step1

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)
    }
    
    func turnOff() {
        self.logic1Subject.onNext(false)
    }

    private func hoge() {
        self.logic1Subject.subscribe { (event) in
            print("hoge")
        }.disposed(by: self.disposeBag)
    }
}

MainModelのturnOnを呼び出すと、コンソールログにhogeと出るはずです。

Step2

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() {
        self.logic1Subject.subscribe { (event) in
            print("hoge1")
        }.disposed(by: self.disposeBag)
        self.logic2Subject.subscribe { (event) in
            print("hoge2")
        }.disposed(by: self.disposeBag)
    }
}

Step3

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)
    }
}
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()
        })
    }
}
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 は反応しない。

Step4

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
            self.isActive.asObservable().subscribe({ (event) in
                observer.onNext(event.element != nil ? event.element! : false)
            }).disposed(by: self.disposeBag)
            return Disposables.create()
        })
    }
}
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)
    }
}

isReady()のonCompletedどうしよう。

let hoge = Observable
    .just([1, 2, 3])
    .map({ (values) -> Int? in
        return values.filter({ (value) -> Bool in
            return value > 2
        }).first
    })
    .filter({ (value) -> Bool in
        return true
    })
    .asObservable()
hoge
    .subscribe { (event) in
        print("hoge2")
    }
    .disposed(by: self.disposeBag)
RxSwiftswiftrx

Background fetch

Rで統計学入門 記録3