Dispatch_Main
Dispatch Main
Info
Serial Queue but async execution if async
is being used. Usually the prefered option.
Usually needed for the system to switch threads
eg. Network call -> Background thread
-> Jump/Transit to Main thread for UI updating tasks.
Warnings
Xcode by default has main thread checker enabled which is a good thing and lets us quickly fix the issue while building the project using simulators.
Settings could be found in Xcode Product scheme -> Run -> Diagnostics ->
Main thread Checker
Publishing changes from background threads is not allowed; make sure to publish values from the main thread (via operators like receive(on:)) on model updates.
UIKit
DispatchQueue.main.async {
self.tableView.reloadData()
}
Combine
Read more about combine thread
Publishers
Sending values from Publishers
.subscribe(on: DispatchQueue.global())
Subscriber
Sink receive values when we are subscriber / observer
.receive(on: Runloop.main)
.receive(on: DispatchQueue.main)
Runloop.main - User interaction | UI gestures will pause changes | execution of code
Updating your UI while scrolling might affect the frames per second (FPS) and smooth scrolling. It could be that your UI update isn’t as necessary when the user is scrolling. In that case, it might make sense to opt-in to using RunLoop.main as a scheduler. - avanderlee
DispatchQueue.main - User interaction won't pause the execution.
RxSwift
RxSwift equivalents Observe
Observe Publisher
.subscribe(on: MainScheduler.instance)
.subscribe(on: MainScheduler.asyncInstance)
Subscriber / Observer
.observe(on: MainScheduler.asyncInstance)
.observe(on: MainScheduler.instance)
Custom Queue scheduler
let scheduler = SerialDispatchQueueScheduler(queue: DispatchQueue.main, internalSerialQueueName: "CustomQueue")
.observe(on: scheduler)
SwiftUI
@MainActor
class CustomClass: ObservableObject {
@Published var data: [Data] = []
init() { fetchData() }
func fetchData() {
data = await URLSession.shared.data(url: "https://sensehack.github.io/")
}
}
Actor & MainActor
Refer this doc for more information
actors
main actor