Instruction

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In this lesson, we’ll go over threads, how to optimize them, and learn more about memory management.

Thread Optimization

Let’s begin by covering the fundamentals before diving into the exciting part of the lesson. So, what exactly is a thread? A thread is a small part of a computer program that runs independently but shares resources, like memory, with other threads in the same program. Think of it as a single task or a mini-program that helps the main program do multiple things at once, or what we call simultaneously, making the program faster and more efficient.

Main vs Background Thread

In iOS, threads can be categorized as either Main or Background threads. The Main thread is responsible for handling tasks related to updating the UI, such as refreshing the screen or updating a UI element. On the other hand, an iOS app can have multiple background threads, limited by the available device resources like CPU and RAM. Background threads are used for non-UI tasks like network requests and heavy computations. It is considered a best practice in iOS development to run as many tasks as possible in the background thread and run the final results to the main thread when updating the UI. Swift provides various methods like GCD (Grand Central Dispatch), Combine, and async/await to move tasks between the main and background threads. In this lesson, we will focus on the newest Swift feature, async/await, which is already widely used in new iOS apps.

Async/Await

To begin, Swift’s async/await syntax is a remarkable addition aimed at simplifying the process of writing and comprehending asynchronous code. By utilizing this feature, developers can write asynchronous code that resembles synchronous code, resulting in improved readability and maintainability. Now, let’s consider a typical Swift method that retrieves data and updates the user interface accordingly:

fetchData { data in
  updateUI(with: data)
}
func fetchData() async -> Data {
  // Asynchronous code to fetch data
}

func updateUI(with data: Data) async {
  // Update UI
}
let data = await fetchData()
Task {
  let data = await fetchData()
  await updateUI(with: data)
}
@MainActor
func updateUI(with data: Data) async {
  // Update UI
}

SwiftUI and ViewModel

SwiftUI aims to simplify the process of managing main and background tasks by minimizing repetitive code. By utilizing the new Observation framework, we can enhance a ViewModel by adding the @Observable attribute. This attribute ensures that all the properties within the ViewModel are automatically updated on the main thread. Consequently, SwiftUI views can effortlessly access and utilize these properties, guaranteeing that they are always up-to-date on the main thread.

Memory Management

The management of memory is a crucial aspect of programming, as it involves handling the life cycles of objects and releasing them when they are no longer necessary. The efficient management of object memory directly impacts the performance of an application. If an application fails to release unnecessary objects, it will gradually consume more memory, leading to a decline in performance.

Automatic Reference Counting (ARC)

Swift uses ARC to automatically manage memory. ARC keeps track of how many strong references an object has and automatically deallocates the object when there are no more strong references to it. This helps in managing memory without the need for manual memory management.

Retain Cycles and Their Impact

A retain cycle occurs when two or more objects hold strong references to each other, preventing them from being deallocated. This can lead to memory leaks, where memory that is no longer needed is not released, causing the application to consume more memory over time and potentially degrade performance.

class Person {
    var apartment: Apartment?
    deinit {
        print("Person is being deinitialized")
    }
}

class Apartment {
    var tenant: Person?
    deinit {
        print("Apartment is being deinitialized")
    }
}

var john: Person? = Person()
var apt: Apartment? = Apartment()

john?.apartment = apt
apt?.tenant = john

john = nil
apt = nil

Breaking Retain Cycles

  1. Using Weak References
class Person {
    var apartment: Apartment?
    deinit {
        print("Person is being deinitialized")
    }
}

class Apartment {
    weak var tenant: Person?
    deinit {
        print("Apartment is being deinitialized")
    }
}

var john: Person? = Person()
var apt: Apartment? = Apartment()

john?.apartment = apt
apt?.tenant = john

john = nil
apt = nil
class Customer {
    let name: String
    var card: CreditCard?

    init(name: String) {
        self.name = name
    }

    deinit {
        print("\(name) is being deinitialized")
    }
}

class CreditCard {
    let number: UInt64
    unowned let customer: Customer

    init(number: UInt64, customer: Customer) {
        self.number = number
        self.customer = customer
    }

    deinit {
        print("Card #\(number) is being deinitialized")
    }
}

var john: Customer? = Customer(name: "John Appleseed")
john?.card = CreditCard(number: 1234_5678_9012_3456, customer: john!)

john = nil

Async/Await Memory Leaks

The usage of async and await can lead to a memory leak situation. This happens when a Task retains a strong reference to self, causing self to remain unreleased until the Task is done, creating a retain cycle. To break this cycle, a weak reference to self is required:

Task { [weak self] in
  guard let data = await self?.fetchData() else { return }
  await self?.updateUI(with: data)
}
See forum comments
Download course materials from Github
Previous: Introduction Next: Demo