Last active
February 18, 2025 16:04
-
-
Save Adobels/f3b345566053f426f51e02dfc704500c to your computer and use it in GitHub Desktop.
Swift Concurrency Task Cases
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
Example 1: | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
task = Task { | |
print("did start first url data request") | |
_ = try await URLSession.shared.data(from: .init(string: "https://httpbin.org/delay/10")!) | |
print("did finish first url data request") | |
print("did start second url data request") | |
_ = try await URLSession.shared.data(from: .init(string: "https://httpbin.org/delay/10")!) | |
print("did finish second url data request") | |
} | |
Task { | |
try await Task.sleep(for: .seconds(1)) | |
task?.cancel() | |
} | |
} | |
Console Output: | |
1. did start first url data request | |
Example 2: | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
task = Task { | |
do { | |
print("did start first url data request") | |
_ = try await URLSession.shared.data(from: .init(string: "https://httpbin.org/delay/10")!) | |
print("did finish first url data request") | |
print("did start second url data request") | |
_ = try await URLSession.shared.data(from: .init(string: "https://httpbin.org/delay/10")!) | |
print("did finish second url data request") | |
} catch { | |
print(error) | |
} | |
} | |
Task { | |
try await Task.sleep(for: .seconds(1)) | |
task?.cancel() | |
} | |
} | |
Colsole Output: | |
1. did start first url data request | |
2. Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLStringKey=https://httpbin.org/delay/10, NSErrorFailingURLKey=https://httpbin.org/delay/10, _NSURLErrorRelatedURLSessionTaskErrorKey=( | |
"LocalDataTask <0E177C95-01EC-43E6-B4E8-68D80B5D6A64>.<1>" | |
), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <0E177C95-01EC-43E6-B4E8-68D80B5D6A64>.<1>, NSLocalizedDescription=cancelled} | |
Example 3 | |
func doSomething() async throws { | |
throw NSError(domain: "", code: 0) | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
task = Task { | |
print("did start first doSomething") | |
_ = try await doSomething() | |
print("did finish first doSomething") | |
} | |
} | |
Console Output: | |
1. did start first doSomething | |
Example 4 | |
func doSomething() async throws { | |
throw NSError(domain: "", code: 0) | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
task = Task { | |
do { | |
print("did start first doSomething") | |
_ = try await doSomething() | |
print("did finish first doSomething") | |
} catch { | |
print(error) | |
} | |
print("did execute last line") | |
} | |
} | |
Console Output: | |
1. did start first doSomething | |
2. Error Domain= Code=0 "(null)" | |
3. did execute last line | |
Example 5: | |
func doSomething() async throws { | |
throw NSError(domain: "", code: 0) | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
task = Task { | |
do { | |
print("did start first doSomething") | |
_ = try await doSomething() | |
print("did finish first doSomething") | |
} | |
print("did start first doSomething") | |
} | |
} | |
Console Output: | |
1. did start first url data request | |
Example 6: | |
func doSomething() async throws { | |
throw NSError(domain: "", code: 0) | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
task = Task { | |
do { | |
print("did start first doSomething") | |
_ = try await doSomething() | |
print("did finish first doSomething") | |
} catch let error as MyError { | |
print("did catch error of type MyError") | |
} | |
print("did finish do catch") | |
} | |
} | |
Console Output: | |
1. did start first doSomething | |
final class pocEscapingTests: XCTestCase { | |
func testDefault() { | |
SnapshotTesting.diffTool = .ksdiff | |
let controller = TotoViewController() | |
/* | |
controller.loadViewIfNeeded() | |
controller.view.drawHierarchy(in: .init(origin: .zero, size: .init(width: 320, height: 450)), afterScreenUpdates: true) | |
controller.viewDidLoad() | |
*/ | |
print("log: \(#function):\(#line)") | |
_ = verifySnapshot(of: controller, as: .image, record: true) | |
print("log: \(#function):\(#line)") | |
} | |
} | |
class TotoViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
print("log: \(#function):\(#line)") | |
let semaphore = DispatchSemaphore(value: 0) | |
print("log: \(#function):\(#line)") | |
let task = Task.detached { | |
print("log: \(#function):\(#line)") | |
let result = try await URLSession.shared.data(from: .init(string: "https://httpbin.org/delay/10")!) | |
print("log: \(#function):\(#line)") | |
semaphore.signal() // Signal that the task is done | |
print("log: \(#function):\(#line)") | |
} | |
print("log: \(#function):\(#line)") | |
semaphore.wait() | |
print("log: \(#function):\(#line)") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment