Skip to main content
deleted 1 character in body
Source Link
lorem ipsum
  • 30.9k
  • 7
  • 42
  • 68

There are 2 kinds of sleep, one for the thread (you are using this one)

https://developer.apple.com/documentation/foundation/thread/1413673-sleep

and the one that shouldmust be used in concurrency.

https://developer.apple.com/documentation/swift/task/sleep(_:)

private func bar() -> AsyncStream<String> { return AsyncStream { continuation in let task = Task{ print("Stream Started") for count in 0...2 { //Concurrency version of sleeping try await Task.sleep(for: .seconds(1)) print("Yielding...") continuation.yield("\(count)") } continuation.finish() } continuation.onTermination = { _ in //Make sure you cancel the task if the stream is terminated task.cancel() } } } 

Concurrency isn't directly related to threads, it is more about "Actors"

https://developer.apple.com/wwdc21/10132

There are 2 kinds of sleep, one for the thread (you are using this one)

https://developer.apple.com/documentation/foundation/thread/1413673-sleep

and the one that should be used in concurrency

https://developer.apple.com/documentation/swift/task/sleep(_:)

private func bar() -> AsyncStream<String> { return AsyncStream { continuation in let task = Task{ print("Stream Started") for count in 0...2 { //Concurrency version of sleeping try await Task.sleep(for: .seconds(1)) print("Yielding...") continuation.yield("\(count)") } continuation.finish() } continuation.onTermination = { _ in //Make sure you cancel the task if the stream is terminated task.cancel() } } } 

Concurrency isn't directly related to threads, it is more about "Actors"

https://developer.apple.com/wwdc21/10132

There are 2 kinds of sleep, one for the thread (you are using this one)

https://developer.apple.com/documentation/foundation/thread/1413673-sleep

and the one that must be used in concurrency.

https://developer.apple.com/documentation/swift/task/sleep(_:)

private func bar() -> AsyncStream<String> { return AsyncStream { continuation in let task = Task{ print("Stream Started") for count in 0...2 { //Concurrency version of sleeping try await Task.sleep(for: .seconds(1)) print("Yielding...") continuation.yield("\(count)") } continuation.finish() } continuation.onTermination = { _ in //Make sure you cancel the task if the stream is terminated task.cancel() } } } 

Concurrency isn't directly related to threads, it is more about "Actors"

https://developer.apple.com/wwdc21/10132

added 120 characters in body
Source Link
lorem ipsum
  • 30.9k
  • 7
  • 42
  • 68

There are 2 kinds of sleep, one for the thread (you are using this one)

https://developer.apple.com/documentation/foundation/thread/1413673-sleep

and the one that should be used in concurrency

https://developer.apple.com/documentation/swift/task/sleep(_:)

private func bar() -> AsyncStream<String> { return AsyncStream { continuation in let task = Task{ print("Stream Started") for count in 0...2 { //Concurrency version of sleeping try await Task.sleep(for: .seconds(1)) print("Yielding...") continuation.yield("\(count)") } continuation.finish() } continuation.onTermination = { _ in //Make sure you cancel the task if the stream is terminated task.cancel() } } } 

Concurrency isn't directly related to threads, it is more about "Actors"

https://developer.apple.com/wwdc21/10132

There are 2 kinds of sleep, one for the thread (you are using this one)

https://developer.apple.com/documentation/foundation/thread/1413673-sleep

and the one that should be used in concurrency

https://developer.apple.com/documentation/swift/task/sleep(_:)

private func bar() -> AsyncStream<String> { return AsyncStream { continuation in let task = Task{ print("Stream Started") for count in 0...2 { //Concurrency version of sleeping try await Task.sleep(for: .seconds(1)) print("Yielding...") continuation.yield("\(count)") } continuation.finish() } continuation.onTermination = { _ in //Make sure you cancel the task if the stream is terminated task.cancel() } } } 

There are 2 kinds of sleep, one for the thread (you are using this one)

https://developer.apple.com/documentation/foundation/thread/1413673-sleep

and the one that should be used in concurrency

https://developer.apple.com/documentation/swift/task/sleep(_:)

private func bar() -> AsyncStream<String> { return AsyncStream { continuation in let task = Task{ print("Stream Started") for count in 0...2 { //Concurrency version of sleeping try await Task.sleep(for: .seconds(1)) print("Yielding...") continuation.yield("\(count)") } continuation.finish() } continuation.onTermination = { _ in //Make sure you cancel the task if the stream is terminated task.cancel() } } } 

Concurrency isn't directly related to threads, it is more about "Actors"

https://developer.apple.com/wwdc21/10132

Source Link
lorem ipsum
  • 30.9k
  • 7
  • 42
  • 68

There are 2 kinds of sleep, one for the thread (you are using this one)

https://developer.apple.com/documentation/foundation/thread/1413673-sleep

and the one that should be used in concurrency

https://developer.apple.com/documentation/swift/task/sleep(_:)

private func bar() -> AsyncStream<String> { return AsyncStream { continuation in let task = Task{ print("Stream Started") for count in 0...2 { //Concurrency version of sleeping try await Task.sleep(for: .seconds(1)) print("Yielding...") continuation.yield("\(count)") } continuation.finish() } continuation.onTermination = { _ in //Make sure you cancel the task if the stream is terminated task.cancel() } } }