This file contains hidden or 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
import Foundation | |
// Create a custom concurrent queue | |
let concurrentQueue = DispatchQueue(label: "com.example.concurrentQueue", attributes:.concurrent) | |
var sharedArray: [Int] = [] | |
let numberOfThreadsAllowedToAccessSharedReource: Int = 1 | |
// Semaphore to control access the shared resource |
This file contains hidden or 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
import Foundation | |
// Create a custom concurrent queue | |
let concurrentQueue = DispatchQueue(label: "com.example.concurrentQueue", attributes:.concurrent) | |
var sharedArray: [Int] = [] | |
// Function to write to the shared array in a thread-safe manner using barrier | |
func writeToArray(element: Int) { | |
concurrentQueue.async(flags: .barrier) { |
This file contains hidden or 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
import Foundation | |
class Search { | |
private var suggestedCitiesWorkItem: DispatchWorkItem? | |
func getSuggestedCities(for prefix: String) { | |
// here cancelling the previous workitem before assigning the new one | |
suggestedCitiesWorkItem?.cancel() | |
let workItem: DispatchWorkItem = DispatchWorkItem { |
This file contains hidden or 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
import UIKit | |
class SplashViewController: UIViewController { | |
var launchDataDispatchGroup: DispatchGroup = DispatchGroup() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
DispatchQueue.global().async { [weak self] in | |
self?.getAppLaunchData() | |
} |
This file contains hidden or 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
import UIKit | |
class SplashViewController: UIViewController { | |
var launchDataDispatchGroup: DispatchGroup = DispatchGroup() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
DispatchQueue.global().async { [weak self] in | |
self?.getAppLaunchData() | |
} |
This file contains hidden or 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
import Foundation | |
// Perform loop 1 to 10 on global concurrent queue with 'userInteractive' QoS | |
DispatchQueue.global(qos: .userInteractive).async { | |
for i in 1...10 { | |
print("Queue 1: \(i)") | |
} | |
} | |
// Perform loop 11 to 20 on global concurrent queue with 'userInitiated' QoS |
This file contains hidden or 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
import UIKit | |
extension UILabel { | |
func updateLabel(with newText: String) { | |
DispatchQueue.main.async { | |
// Update the text of the label on the main thread | |
self.text = newText | |
} | |
} | |
} |
This file contains hidden or 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
const Parent = () => { | |
const [count, setCount] = useState<number>(0); | |
const printFunction = useCallback((text: string) => { | |
console.log(text); | |
}, []); | |
// some other functions | |
return ( |
This file contains hidden or 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
type props = { | |
callback: (a: string) => void; | |
} | |
const Child: React.FC<props> = ({callback}) => { | |
const [value, setVlaue] = useState<number>(0); | |
const isEven = () => { | |
let i = 0; | |
// this while loop is to mock a costly operation |
This file contains hidden or 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
const Parent = () => { | |
const [count, setCount] = useState<number>(0); | |
const printFunction = (text: string) => { | |
console.log(text); | |
} | |
// some other functions | |
return ( |