Skip to content

Instantly share code, notes, and snippets.

@khawajafarooq
Created December 15, 2018 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khawajafarooq/1e7de4ccc8aae74d1adc8e18105d4dda to your computer and use it in GitHub Desktop.
Save khawajafarooq/1e7de4ccc8aae74d1adc8e18105d4dda to your computer and use it in GitHub Desktop.
Demonstration of app threads in swift
// Application threads
// Background
func BG(_ block: @escaping () -> Void) {
DispatchQueue.global(qos: .default).async(execute: block)
}
// Main
func UI(_ block: @escaping () -> Void) {
DispatchQueue.main.async(execute: block)
}
// Example
BG { [unowned, weak self] in
// load data on background thread
self.viewModel.loadData() { [weak self] data in
UI {
// Updating UI
self?.updateData(data)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment