Junior — Middle
Can you explain the concept of concurrency and its role in programming?
sobes.tech AI
Answer from AI
Concurrency is the ability of a program to perform multiple tasks simultaneously or to overlap their execution in time. In programming, this is important for improving resource utilization, enhancing application responsiveness, and speeding up data processing.
In iOS, for example, concurrency helps perform network requests, data processing, and UI updates in parallel without blocking the main thread. Tools such as GCD (Grand Central Dispatch) and OperationQueue are used for this purpose.
An example of using GCD for an asynchronous task:
DispatchQueue.global(qos: .background).async {
// Long-running task
let result = performHeavyCalculation()
DispatchQueue.main.async {
// UI update
self.label.text = "Result: \(result)"
}
}