Код: Выделить всё
import SwiftUI
struct ContentView: View {
@State private var scale: CGFloat = 1.0
var body: some View {
VStack {
Text("Animating Text")
.font(.largeTitle)
.scaleEffect(scale)
.animation(
Animation.easeInOut(duration: 1.5)
.repeatForever(autoreverses: true)
)
.onAppear {
self.scale = 1.5
}
Button("Run With DispatchQueue") {
DispatchQueue.global(qos: .background).async {
performHeavyLoad(duration: 60 * 5)
}
}
Button("Run With Task") {
Task(priority: .background) {
// Example: High-load computation for 5 minutes
performHeavyLoad(duration: 60 * 5)
}
}
}
}
// Function to perform heavy computation on the calling thread
nonisolated
func performHeavyLoad(duration: TimeInterval) {
let start = Date()
let end = start.addingTimeInterval(duration)
while Date() < end {
for _ in 0..
Подробнее здесь: [url]https://stackoverflow.com/questions/78651075/comparing-dispatchqueue-and-task-for-running-heavy-computations-in-the-backgroun[/url]