Skip to content

Instantly share code, notes, and snippets.

@maedaunderscore
Created October 29, 2020 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maedaunderscore/9c648511bec82c77700392a09a1539c4 to your computer and use it in GitHub Desktop.
Save maedaunderscore/9c648511bec82c77700392a09a1539c4 to your computer and use it in GitHub Desktop.
swift + CombineフレームワークのFuture + DispatchQueueを組み合わせる
import Combine
import SwiftUI
func run1() -> Future<String, Never>{
return Future{ promise in
promise(.success("OK1: \(Thread.isMainThread)"))
}
}
func run2() -> Future<String, Never>{
return Future{ promise in
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
print("OK2: \(Thread.isMainThread)")
promise(.success("OK3: \(Thread.isMainThread)"))
}
}
}
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
print("OK4: \(Thread.isMainThread)")
}
run1().sink { print("data: \($0)")}
run2().sink { print("data: \($0)")}
data: OK1: true
OK2: true
OK4: true // <- OK2が出てほしい
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment