loading

Multi Threading in swift

Multithreading in swift

The technique that enables the CPU to construct and run parallel threads is known as multithreading. A CPU normally completes one task at a time. Nevertheless, we may enable the CPU to transition between various activities so that they can be completed concurrently by employing multithreading.

The user is not aware of the change because the threads are switched between quickly. One such example in iOS is the UI thread, which maintains the application’s User interface even while the CPU is engaged with other tasks. The reason for this is multithreading.

The UI on iOS has a thread. The user interface doesn’t alter while we perform a complicated activity. Consider the following scenario: we run the app in the foreground while carrying out certain uploading operations in the background. Here, the iPhone rapidly flips between two threads until they are finished. Since the iPhone may alternate between uploading the file and drawing the screen, we don’t need to complete the tasks in order.

GCD Concepts

Grand-Central-Dispatch, or GCD for short, is an API that is used to carry out the worker pool closures. In this instance, the First-In-First-Out (FIFO) order of execution is followed.

The application submits the tasks that need to be done by the CPU in the form of blocks to the dispatch queue. The system provides a thread pool on which this block is run. Every task in the dispatch queue is carried out either simultaneously or sequentially. Nonetheless, the jobs are always kept in FIFO order by the dispatch queue.

By sending a job to the system’s dispatch queues, this framework makes it easier to run code concurrently on a multi-core system.

Why use GCD?

These days, the most common practice is to keep the application user-responsive. But in order to satisfy our needs, we never stop adding new features to our application, which makes it less intuitive to use and slower.

We can utilize GCD to run the application threads and show the user the information in order to make the application more responsive and faster. For instance, we can always start a thread that shows the user an activity indicator to make it appear as though some tasks are being processed if we are processing a large JSON file that takes ten or fifteen seconds to parse.

Let’s see an example where a task will be asynchronously dispatched to the main queue.

				
					import UIKit  
  
  
class ViewController: UIViewController {  
  
  
    @IBOutlet weak var tableView: UITableView!  
      
    let refreshControl = UIRefreshControl()  
    var arr = Array<String>()  
      
    override func viewDidLoad() {  
        super.viewDidLoad()  
        // Do any additional setup after loading the view.  
        var n: Int = 2  
        var value:Int = 1  
  
  
        DispatchQueue.main.async {  
            debugPrint("printing table of \(n)")  
            for i in 1...10 {  
                value = i  
                  
                print(n*i)  
            }  
        }  
  
  
        for i in 0...10 {  
            n = i  
            print("Value = " + n.description)  
        }  
  
  
        DispatchQueue.main.async {  
            n = 9  
            print(n)  
        }  
    }  
}  
				
			

Output

Value = 0
Value = 1
Value = 2
Value = 3
Value = 4
Value = 5
Value = 6
Value = 7
Value = 8
Value = 9
Value = 10
"printing table of 10"
10
20
30
40
50
60
70
80
90
100
9
Share this Doc

Multi Threading in swift

Or copy link

Explore Topic