Last active
February 7, 2022 14:52
-
-
Save SebastianBoldt/5dfcbef6fc3a3cce84f65472f7150107 to your computer and use it in GitHub Desktop.
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
class DelegateWrapper: NSObject, ObservableObject, CLLocationManagerDelegate { | |
private var continuation: CheckedContinuation<Data?, Error>? | |
private let dataService = SomeClassThatReturnsData() | |
override init() { | |
super.init() | |
dataService.delegate = self | |
} | |
func requestData() async throws -> Data? { | |
try await withCheckedThrowingContinuation { continuation in | |
continuation = continuation | |
dataService.requestData() | |
} | |
} | |
func receivedData(_ data: Data) { | |
continuation?.resume(returning: data) | |
} | |
func receivedError(_ error: Error) { | |
continuation?.resume(throwing: error) | |
} | |
} | |
struct ContentView: View { | |
@StateObject private var delegateWrapper = DelegateWrapper() | |
var body: some View { | |
Button("Load Data") { | |
Task { | |
if let data = try? await delegateWrapper.requestData() { | |
print("data: \(data)") | |
} else { | |
print("no data.") | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment