-
Multithreading is mostly about "queues" in iOS. Functions (usually closures) are simply lined up in a queue. Then those functions are pulled off the queue and executed on an associated thread(s). Queues can be "serial"(one closure a time) or "concurrent"(multiple thread servicing it).
-
Main queue: All UI activity MUST occur on this queue and this queue only. And, conversely, non-UI activity that is at all time consuming must NOT occur on that queue. We do this because we want our UI to be highly responsive. And also because we want things that happen in the UI to happen predictably (serially).
let mainQueue = DispatchQueue.main
let backgroundQueue = DispatchQueue.global(qos: DispatchQos)
DispatchQos.userInteractive // high priority, only do something short and quick, very rare to use
DispatchQos.userInitiated // high priority, but may take a little bit of time
DispatchQos.background // not directed initiated by user, so can run as slow as needed
DispatchQos.utility // long-running background processes, low priority
- There are two primary ways of putting a closure onto a queue.
// You can plop a closure onto a queue and keep running on the concurrent queue
queue.async { ... }
// or you can block the concurrent queue waiting until the closure finishes on that other queue.
queue.sync { ... }
// we almost always do the former
// we have weakSelf here is not because this closure causes a memory cycle
// it's because if the viewController is nil somehow, we don't want the closure the keep the pointer in the heap
// when doing multi-threading stuff, always be aware of timing issue
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
let urlContents = try? Data(contentsOf: url)
DispatchQueue.main.async {
if let imageData = urlContents, url == self?.imageURL {
self?.image = UIImage(data: imageData)
}
}
}
- All iPhones in portrait are compact in width and regular in height
- iPhone Plus is also compact in width and regular in height in portrait. But in landscape, the iPhone Plus is only compact in height.
- iPad is always regular in both dimensions. But depending on the environment an MVC is in, it might be compact(e.g. split view master)