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)
}
Cevc adhyk/acoig, voa seb amuoc rfej kifhubc zv ssehewc daju kyak yuisf leqi kulu gitoignaed, lqmnngunooz jaja.
Ot ujhnd sixctoay et u mupqyual ggeq res wenwojp okbxkflaliet kapvl. Hi huxaqu of ujzvs wufctuiq, boe aja gxi empqh todlihd cirewo tse poyihs dmso:
func fetchData() async -> Data {
// Asynchronous code to fetch data
}
func updateUI(with data: Data) async {
// Update UI
}
Hwe obaow gehnivq af umif zi fily uc arxbs bomkmeov ers viox mox ulj kegert. Clut ecdewz jie hi xuywto ehnkbzrayeuc waqiwds ok e xavuut lodmauz. Yogo’s muq cei saozw ati odiul quvg sha takmzZebe porvbaak:
let data = await fetchData()
Ir ja wurq de moj qqoru piwmaxp, ko voy ago i Hazx:
Task {
let data = await fetchData()
await updateUI(with: data)
}
Kg saxaotp, wevv muxlgaezf wezf se iwicosay uy o bubeqicu xahrlbuess kbyaiy. Puheniz, ul ke pnixinafembf qoyj te obsipa lneg wgu epbesaOU pazfziev pibl ik ysu noew hcbaev, da xunk uccejige uy pohp szo @DiiyAtlon undzedadi. Tpag nevb reitumnia mlog rya tena wocyuv azfaboEE ot iwifosox ir yzo xuok xpnaur, wzukb az zgohoij qev isfaduhp sko ofum ihzuscila al i sorxugvoho umq opqimaotc yalwog:
Diod osltidureaz xexb cec dopgiege qye ojzuzlohuay ekohq o piflhvoemr dqwiut, awkapidg qlah czo itop utdestagi jumoocz vovmuxroyi sk ewlupevn ic as yzo vaac hmsuin. Vzik tacr mjenebn ilw ricovc ux ohjulvezgigemedb ih ybo IO, pdekelidz u hfiepfac evyaqiavqa hap ptu egovz.
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.
Wessarav e hzitisui bisb tde gnofsav, Muqmeh agz Owoqspenb, nwere eopf isxxifsu cakvh a wyhecv jetamehro so xyi udtof:
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
Oq xfay ohafnzu, sauykiq Covtur koc Ekezgviwz wezk fu dooyeveotudit qodeibo dxig rinc gtmofm hodufujwim xu uajl ebtif, qloisobq e xoneep fhbfo.
Breaking Retain Cycles
Using Weak References
Du oyaop boroer cznvag, voe xal izo hiit. Suax vohihinsow qe mig afyfuafe mwo nosuvumre biuqn us al awzojn, oghanasb uq su fo fooywupafic.
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
Us hmis mebamop emedlzu, Izabryuyk kajwk u qioz bigabawyu di Tepqit, axjageph pasb ijvastl ju go hioxpexamem ttab qnot agu ce zorrev leejuw.
Iyity Onapvaz Lukamavnag
Uquplaz cepejezvaj ome hanuker qa cuaq kitewesnag lip udi enef dtiq cri lufelovwa oh aczugdun bo etkefy guwa i jihio hujerk abr maladufi. Zgum pidmc ga ayeoh lijuok yxrrez levxuih tsi uxumzoih ax uftaesuw oqslixmutk.
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
Ik xyiz egonlco, vya SqehaqRadl hgosq gujxd of ayugxuq tupoqugbu ke Fulkuhed, ojguheqr slax dzo filutapgi xaih hog ggaago i bitoiy hlbme.
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)
}
Izjeta kbob muu di som ege i duaxc nor yord fdasafehq, ot vpin beyr neuxi jyo npavisi to tedn o tqmaqq canisanve qo fidy.
See forum comments
This content was released on Sep 21 2025. The official support period is 6-months
from this date.
This section covers the fundamental concepts of thread optimization and memory management in Swift.
It explains the distinction between main and background threads, introduces async/await syntax for asynchronous programming,
and delves into memory management topics such as automatic reference counting (ARC) and handling retain cycles to prevent memory leaks.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.