Grand-central-dispatch-in-swift-fdfdd8b22d52.pdf

  • Uploaded by: Arav Singh
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Grand-central-dispatch-in-swift-fdfdd8b22d52.pdf as PDF for free.

More details

  • Words: 532
  • Pages: 4
Homepage

Get started Anand Nimje I am Swift developer and makes iOS apps Feb 4 · 2 min read

Grand Central Dispatch in Swift Multi threading? Concurrency? Asynchronous task? GCD (Grand Central Dispatch) ? in Swift Programming | Feb 4th 2018

Introduction of Grand Central Dispatch Grand Central Dispatch or GCD is a low-level API for managing concurrent operations. It will make your application smooth and more responsive. Also helps for improving application performance. Sometimes we are trying to perform multiple tasks at the same time that time most of the developer-facing application hang or freezing issue this is the common issue. That’s why we are using GCD to manage multiple tasks at the same time.

. . . Most of the time you probably used this code frequently DispatchQueue.main.async { // Perform your async code here }

DispatchQueue The DispatchQueue API like a company . Who having staff units like junior level and

senior level workers . So now the company can take both heavy and light work with there team. One more important thing here its manage task in FIFO Order.

Concurrenct It’s starting multiple tasks at the same time but not guarantee for the finish at same time. Its can finish any order.

Serial It’s executing one task at a time.

Sync vs Async Sync -When a work item is executed synchronously with the sync method, the program waits until execution finishes before the method call returns. 1

func syncWork(){

2

let northZone = DispatchQueue(label: "perform_task_with_team_north")

3

let southZone = DispatchQueue(label: "perform_task_with_team_south")

4 5

northZone.sync {

6

for numer in 1...3{ print("North \(numer)")}

7

}

8

southZone.sync {

9

for numer in 1...3{ print("South \(numer)") }

10

}

11

}

12

Async -execute asynchronously with the async method, the method call returns immediately. 1

func asyncWork(){

2

let northZone = DispatchQueue(label: "perform_task_with_team_north")

3

let southZone = DispatchQueue(label: "perform_task_with_team_south")

4 5

northZone.async {

6

for numer in 1...3{ print("North \(numer)") }

7

}

8

southZone.async {

9

for numer in 1...3{ print("South \(numer)") }

10 11

} }

12

. . . Code Example Perform network task with UI updates Global Queue -Using to perform non-UI work in the background. Main Queue -Using to update the UI after completing work in a task on a concurrent queue. List of DispatchQueue Priority -

.userInteractive .userInitiated .default .utility .background .unspecified

Perform background Quality of Service (QOS) task -

DispatchQueue.global(qos: .background).async { // Call your background task DispatchQueue.main.async { // UI Updates here for task complete. } }

DispatchQueue with delay let deadlineTime = DispatchTime.now() + .seconds(1) DispatchQueue.main.asyncAfter(deadline: deadlineTime) { //Perform code here }

Dispatch Groups let dispatchGroup = DispatchGroup() dispatchGroup.enter() loadUseractivities { dispatchGroup.leave() } dispatchGroup.enter() loaduserComments { dispatchGroup.leave() } dispatchGroup.notify(queue: .main) { print("all activities complete ") }

. . . Conclusion Finally we can say Grand central dispatch is the powerful api for multitasking with sync and async programming in iOS. I hope you will like this.

. . . Thank you ! If you having any query regarding this tutorial ? Twitter me: @Anand iOS

Swift

Programming

Xcode

Multithreading

591

Anand Nimje

1

Follow

I am Swift developer and makes iOS apps

Never miss a story from Anand Nimje

GET UPDATES

More Documents from "Arav Singh"