2017-07-11 3 views
1

アプリケーションがフォアグラウンドに入るときにタスクを実行します。 私はビューモデルが初期化されるとすぐにこのタスクを実行したいと思います。RxSwift最適化トリガー

タスクコードのコピーと貼り付けを避けるにはどうすればよいですか? 現在のコードは次のようになります。

init(dependencies: Dependencies) { 
     self.dependencies = dependencies 

     dependencies.apiClient.notificationsCount() 
      .map { $0.value > 0 ? String($0.value) : nil } 
      .bind(to: tabBadgeValue) 
      .disposed(by: disposeBag) 
     dependencies.notification.notification(for: .appWillEnterForeground) 
      .map { _ in() } 
      .flatMapLatest(dependencies.apiClient.notificationsCount) 
      .map { $0.value > 0 ? String($0.value) : nil } 
      .bind(to: tabBadgeValue) 
      .disposed(by: disposeBag) 
    } 

答えて

1

あなたが最初の通知を受ける前に、次のイベントを発するようにstartWithを使用することができます。

init(dependencies: Dependencies) { 
    self.dependencies = dependencies 

    dependencies.notification.notification(for: .appWillEnterForeground) 
     .map { _ in() } 
     .startWith(()) 
     .flatMapLatest(dependencies.apiClient.notificationsCount) 
     .map { $0.value > 0 ? String($0.value) : nil } 
     .bind(to: tabBadgeValue) 
     .disposed(by: disposeBag) 
} 
+0

これはまさに私が探していたそのためのソリューションです。 –