Browse Questions
  • setNeedsLayout schedules a layout pass for later, layoutIfNeeded forces any pending layout to happen immediately, and setNeedsDisplay schedules a redraw of the view's content.
  • - Instruments — CPU Profiler, Time Profiler, Allocations, Leaks, Core Data - Xcode Memory Graph — find retain cycles and leaked objects - View Hierarchy Debugger — spot off-screen renders,…
  • SOLID is a set of five design guidelines for maintainable OOP code: | Principle | Idea | Swift application | |--|--|--| | Single Responsibility | A type should have one reason to change | Split a…
  • A race condition occurs when two or more threads access shared mutable state simultaneously, producing unpredictable results depending on timing.

Answer: A race condition occurs when two or more threads access shared mutable state simultaneously, producing unpredictable results depending on timing.

Code Example:

// ❌ Race condition — counter could be corrupted
class UnsafeCounter {
    var count = 0
    func increment() { count += 1 }  // not thread-safe
}

// ✅ Option 1 — Use an actor (preferred in modern Swift)
actor SafeCounter {
    var count = 0
    func increment() { count += 1 }  // actor serializes access
}

// ✅ Option 2 — Serial dispatch queue
class QueueCounter {
    private var count = 0
    private let queue = DispatchQueue(label: "com.app.counter")

    func increment() {
        queue.async { self.count += 1 }
    }

    func value(completion: @escaping (Int) -> Void) {
        queue.async { completion(self.count) }
    }
}