2017-12-19 21 views
0

그래서 아래의 코드는 errror로 컴파일RxSwift : 배치 (기준 : 이상한 것들

var doneSubscription: Disposable = item.doneSubjectObservable 
     .debug("doneSubscriptions") 
     .subscribe(
      onNext: { 
       done in self.validateDone(done: done, item: item) 
     }).disposed(by: disposeBag) 

Value of type '()' does not conform to specified type 'Disposable' on the line .disposed(by: disposeBag)

하지만 오류없이이 작업을 수행 할 수 있습니다

var doneSubscription: Disposable = item.doneSubjectObservable 
     .debug("doneSubscriptions") 
     .subscribe(
      onNext: { 
       done in self.validateDone(done: done, item: item) 
     }) 

    doneSubscription.disposed(by: disposeBag) 

내가했던 모든이 서브 스크립 션 체인에서 .disposed(by: disposeBag)을 이동했습니다.

내가 누락 되었습니까?이 두 가지 접근 방식이 동일하지 않습니까?

답변

4

아니요, 이들은 동일하지 않습니다. 이 당신에게 오류를 제공, 아무것도 반환하지 않습니다 disposed(by:) 때문에

item.doneSubjectObservable 
    .debug("doneSubscriptions") 
    .subscribe(
     onNext: { 
      done in self.validateDone(done: done, item: item) 
    }).disposed(by: disposeBag) 

: 첫 번째 경우

, 당신은 doneSubscription에 입력 Disposable의 변수를이 모든 표현의 반환 값을 저장한다. 그러나 두 번째 경우에는 실제로 Disposable을 변수 doneSubscription - subscribe의 반환 값에 할당했습니다.

첫 번째 경우를 수정하려면 변수 선언을 제거하기 만하면됩니다.

+0

간단하면서도 완벽한 설명 :) 그러므로 +1 –