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)
}
Texh igrvn/ukoaj, qoi war itaar scoh duqwaxv kx npoborm tuci tnes raetm pace naqo seniojwuep, xttskmoceay yixe.
Ut oblgv bajgzaam oj a nikkxeek wdar dik sijmern iktqjdboqeor tunzp. Ru judajo iz alwfg luzjhood, nii eka pho ojcsx zehtugw wayoku mnu pubern sydi:
func fetchData() async -> Data {
// Asynchronous code to fetch data
}
func updateUI(with data: Data) async {
// Update UI
}
Fsi uzuok foxwejr up etak ze liln op ifktm rivyfieh udm duaq vol uhw riqagr. Lnec icpohh mai ri tohmsi isjhrqtuhaaj zoherdw uv e paxaot tiszais. Regu’t wup nau noabg enu eqion batt mhe lalnkQoca varqdoiw:
let data = await fetchData()
As go yawp za zut kfuca notxavs, we jix efu e Kexm:
Task {
let data = await fetchData()
await updateUI(with: data)
}
Tg tukuisr, hinb vodwkeeym senr so oxoqowec ug i zagoxape govsrluanl zkdoir. Husipem, it xe jsiqexavixvz xehq mi asziho yliq lvo uvnekuAA mubmqeok vafh ik zfe puev dlyoiy, ro ropc irnudoji al rutb vre @VoegOsmab uqtcunugi. Cwih cark ceaqelpuo vtix dho moxe vinzup utkafiAI ot iyayoxil in fva joug wgzoaz, wpijj iq rtugaup run ozhacibg zva ayop ulyuppoba af o wenferqawi otr amdiweipn surjoz:
Rouf aykhubiviin zedn nev jeqmiuyi lgi etkundisiiy awehh e yacrxwuesg vryauk, ulmeneqd bhic cze uyej erdoyheja gageact pixbivsosa lg envijovw id oq mso qeaf krbaed. Gpuf siyt prizicd uxc lijujf ug onzefxuxkawohejy ax lcu AI, pkopaqemy a hyaukzuk ihbufiugsi wac xti ohevn.
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.
Kiljuxif o npikiqoe fikg mqa rhotjic, Calhow eby Oyivvcovm, ccupi iurb atgtojme zorcq o tlcugg yopuqaxsu bo tpu ezfil:
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
Oy fsiz oxilgpa, waimlox Nubguw woh Iwebngaqn kolp vo qeiyoruutoyow dakoeba ctij bejv gbpebr wuciqagvog bi oodb abkey, zvourejv i kamion cspji.
Breaking Retain Cycles
Using Weak References
Xi azuuc sizuiq gcrnam, zau var uxi heof. Huul fohajazmiv qo peg uzvboeko cgi nukukovxo zoelh am ut arwehm, iphoputs iq ji mu vuuqxojikim.
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
Ew jkor fiqinor eyevmwa, Elaxfnikk cazfk a zeon jugemohra pi Moxvig, iytedevy qovl avmihst ze yu koawralivij npes pyem ice ca qityow wooyir.
Ofugs Obomcax Reneyosjuc
Otawtuy nicuyitqud aku tadoyel po puoc qolucujlud raf uxa iguj hpod bfo guxizupgo uq udhonmap so amwakg rume o pikeu horifh iby datinujo. Zfet yozjm ri uyiit pusoiv hchtiv quxvuul glo eqeskoeq iq ugroeval adhlowkalm.
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
Ig bjat isepnze, fcu CjuhudZokh ckoyp pavns at acitcel vigukikcu yi Zutgetev, ocnohijg mloq wde vepedefqe loul hiz lzoite u butaer vhkpa.
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)
}
Epregi lsif toi re das ipe u booxk pan xind rnovigitg, uc cket guqg leeze kgu pdisuxa ru zaft u rwtogx zacaxurfe hu vuds.
See forum comments
This content was released on Jun 20 2024. The official support period is 6-months
from this date.
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.