RxSwift withLatestFrom с resultSelector не компилируется - PullRequest
2 голосов
/ 10 мая 2019

У меня есть Driver типа Bool и BehaviorRelay типа Page (который является пользовательским перечислением).

enum Page {
    case option1(CustomClass1, CustomClass2)
    case option2(CustomClass3)
    case option3(CustomClass4)

    var property1: CustomClass2? {
        switch self {
        case .option1(_, let custom):
            return custom
        case .option2, .option3:
            return nil
        }
    }
}

У меня есть Driver<Bool> в другой ViewModel.

class ViewModel1 {
    struct Output {
        let hasItems: Driver<Bool>
    }

    let output: Output

    init() {
        let hasItemsRelay: BehaviorRelay<Bool> = BehaviorRelay<Bool>(value: false)

        self.output = Output(
            hasItems: hasItemsRelay.asDriver()
        )
    }
}

И у меня есть BehaviorRelay<Page?> в моем базовом классе.

class ViewModel2 {
    let currentPageRelay: BehaviorRelay<Page?> = BehaviorRelay<Page?>(value: nil)

    init() {
        self.currentPageRelay = BehaviorRelay<Page?>(value: nil)
    }
}

В классе ViewModel2 я пытаюсь перехватить событие на hasItems драйвере ViewModel1.Input, и когда я получаю событие, мне нужно текущее значение currentPageRelay, а затем делать что-то с ним. Так что в основном withLatestFrom это то, что мне нужно использовать.

class ViewModel2 {

   private func test() {
       let customViewModel: ViewModel1 = ViewModel1()

       customViewModel
           .output
           .hasItems
           .withLatestFrom(currentPageRelay) { ($0, $1) }
           .map { (hasItems, page) -> (CustomClass2, Bool)? in 
               guard let property1 = page?.property1 else { return nil }
               return (property1, hasItems)
           }
           .unwrap()
           .drive(onNext: { (property1, hasItems) in 
               // do stuff
           }
           .disposed(by: disposeBag)
   }
}

Xcode полностью теряет его на withLatestFrom. Нет завершения кода, и это дает следующую ошибку компиляции: Expression type '(Bool, _)' is ambiguous without more context

Я полностью в неведении об этом. Я уже все перепробовал, предоставив правильные классы в списке параметров под ним, чтобы он знал, чего ожидать и т. Д., Но пока не повезло.

1 Ответ

2 голосов
/ 10 мая 2019

Добавьте .asObservable() после .hasItems:

class ViewModel2 {
    let currentPageRelay: BehaviorRelay<Page?> = BehaviorRelay<Page?>(value: nil)
    let disposeBag = DisposeBag()

    init() {
        // self.currentPageRelay = BehaviorRelay<Page?>(value: nil)
    }

    private func test() {
        let customViewModel: ViewModel1 = ViewModel1()

        customViewModel
            .output
            .hasItems
            .asObservable()
            .withLatestFrom(currentPageRelay) { ($0, $1) }
            .map { (hasItems, page) -> (CustomClass2, Bool)? in
                guard let property1 = page?.property1 else { return nil }
                return (property1, hasItems)
            }
            .asDriver(onErrorJustReturn: nil)
            .drive(onNext: {
                guard let (property1, hasItems) = $0 else {
                    return
                }
                // do stuff
            })
            .disposed(by: disposeBag)
    }
}

...