Skip to content

Instantly share code, notes, and snippets.

@takasek
Created December 5, 2017 03:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takasek/f39e606d09a3f0e5484ba227e7f9f286 to your computer and use it in GitHub Desktop.
Save takasek/f39e606d09a3f0e5484ba227e7f9f286 to your computer and use it in GitHub Desktop.
RxSwiftのflatMapFirstで `isLoading` 的なフラグ管理相当のことができることの確認 #CodePiece
let trigger = PublishSubject<Void>()
func fetch() -> Single<String> {
return Single.create { observer in
print("🛫")
DispatchQueue.global().async {
usleep(2_500_000)
print("🛬")
observer(.success("レスポンス"))
}
return Disposables.create()
}
}
trigger
.debug("▶️")
.flatMapFirst { fetch() }
.subscribe(onNext: { print("👀", $0) })
trigger.onNext(())
sleep(1)
trigger.onNext(())
sleep(1)
trigger.onNext(())
sleep(1)
trigger.onNext(())
sleep(1)
trigger.onNext(())
/*
2017-12-05 12:34:11.546: ▶️ -> subscribed
2017-12-05 12:34:11.548: ▶️ -> Event next(())
🛫
2017-12-05 12:34:12.551: ▶️ -> Event next(())
2017-12-05 12:34:13.554: ▶️ -> Event next(())
🛬
👀 レスポンス
2017-12-05 12:34:14.557: ▶️ -> Event next(())
🛫
2017-12-05 12:34:15.560: ▶️ -> Event next(())
🛬
👀 レスポンス
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment