Created
December 15, 2018 14:43
-
-
Save khawajafarooq/1e7de4ccc8aae74d1adc8e18105d4dda to your computer and use it in GitHub Desktop.
Demonstration of app threads in swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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