g5321247 / Practice_GCD

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GCD Test

GCD is a thread control tool written in Swift.

Features

  • Fetch

    Fetch Data from the Internet and display it by order

Method

  • Group
  • Semaphore
  • DeadLock
  • Deadlock Prevention

1. Group

Declare the variable - dispatchGroup and implement dispatchGroup enter and leave in completion handler, to make sure all data will settle down before reloading it.

let dispatchGroup = DispatchGroup()

dispatchGroup.enter()

let dataTask = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in

......

guard let response = response as? HTTPURLResponse else {               

......

self.dispatchGroup.leave()

}

It will notify us once all the process finished.

self.dispatchGroup.notify(queue: .main){

self.onlineData.append(result)

self.tableView.reloadData()

}

2. Semaphore

About


Languages

Language:Swift 100.0%