Swift Views Shouldn’t Know About Threads
Think about this one
2 min readJun 21, 2022
Views should be rather basic, right. Have we thought about
Read on to find out all the details!
Difficulty: Beginner | Easy | Normal | Challenging
This article has been developed using Xcode 13.1, and Swift 5.5.1
Terminology
Thread: Tasks can have their own threads of execution to stop long-running tasks block the execution of the rest of the application
The motivation
Lots of my code has DispatchQueue.main.async
to put us onto the main thread for UI work.
So this means that I might be communicating between a view model and view controller through a closure so I might have some code like the following:
self.viewModel.dictionarySet = { dict in
let values = dict.allValues.description
DispatchQueue.main.async {
self.textView.text = values
}
}