Created
November 30, 2022 07:35
-
-
Save fishkingsin/17e3fed35fcb59ab6df9aac8bb87e473 to your computer and use it in GitHub Desktop.
testing withUnsafeThrowingContinuation
This file contains hidden or 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 UIKit | |
func printMessage() async { | |
let max: UInt32 = 4 | |
let min: UInt32 = 0 | |
let myRange: Range = min..<max | |
let string = await withTaskGroup(of: String.self) { group -> String in | |
group.addTask { | |
sleep(UInt32.random(in: myRange)) | |
return "Hello" | |
} | |
group.addTask { | |
sleep(UInt32.random(in: myRange)) | |
return "From" | |
} | |
group.addTask { | |
sleep(UInt32.random(in: myRange)) | |
return "A" | |
} | |
group.addTask { | |
sleep(UInt32.random(in: myRange)) | |
return "Task" | |
} | |
group.addTask { | |
sleep(UInt32.random(in: myRange)) | |
return "Group" | |
} | |
var collected = [String]() | |
for await value in group { | |
collected.append(value) | |
} | |
return collected.joined(separator: " ") | |
} | |
print(string) | |
} | |
Task { | |
await printMessage() | |
} | |
let queue = DispatchQueue(label: "doSomthing") | |
func doSomething(_ completion: @escaping (_ result: Result<String, Error>) -> Void) { | |
queue.async { | |
let max: UInt32 = 1 | |
let min: UInt32 = 0 | |
let myRange: Range = min..<max | |
sleep(UInt32.random(in: myRange)) | |
let rand = Int.random(in: 0..<10) | |
switch rand { | |
case let r where r == 3: | |
print("no return") | |
break | |
case let r where (r%2 == 0): | |
completion(.success("Hello \(r)")) | |
default: | |
completion(.success("World \(rand)")) | |
} | |
} | |
} | |
func waitForDataStoreReady() async throws -> String { | |
let lock = NSLock() | |
return try await withUnsafeThrowingContinuation { (continuation: UnsafeContinuation<String, Error>) in | |
var nillableContinuation: UnsafeContinuation<String, Error>? = continuation | |
doSomething { result in | |
lock.lock() | |
defer { lock.unlock() } | |
switch result { | |
case .success: | |
nillableContinuation?.resume(with: result) | |
nillableContinuation = nil | |
case .failure(let error): | |
nillableContinuation?.resume(throwing: error) | |
nillableContinuation = nil | |
} | |
} | |
} | |
} | |
Task { | |
do { | |
var result = try await waitForDataStoreReady() | |
print(result) | |
result = try await waitForDataStoreReady() | |
print(result) | |
result = try await waitForDataStoreReady() | |
print(result) | |
result = try await waitForDataStoreReady() | |
print(result) | |
result = try await waitForDataStoreReady() | |
print(result) | |
result = try await waitForDataStoreReady() | |
print(result) | |
result = try await waitForDataStoreReady() | |
print(result) | |
result = try await waitForDataStoreReady() | |
print(result) | |
result = try await waitForDataStoreReady() | |
print(result) | |
} catch let exception { | |
print(exception) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment