Created
October 29, 2020 09:27
-
-
Save maedaunderscore/9c648511bec82c77700392a09a1539c4 to your computer and use it in GitHub Desktop.
swift + CombineフレームワークのFuture + DispatchQueueを組み合わせる
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)")} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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