Sie sind auf Seite 1von 3

Concurrency in iOS Development

Concurrency means the collection of techniques and mechanisms that enable a


system to perform several different tasks simultaneously. Concurrency is considered
hard to understand. It helps to write powerful, fast execution, and reactive apps. In iOS
we have NSOperation and Dispatch Queues to perform concurrency.

● Utilize device hardware: Now all devices have a multi-core processor that allows
developers to execute multiple processes in parallel. We should utilize this
feature and get benefits out of the hardware.
● Better user experience: While performing heavy tasks UI will freeze till the task
execution. When the user faces this situation, the first step that user will take is to
uninstall/close your app without a second thought. With concurrency, all these
tasks can be done in the background without changing the main thread or
disturbing your users. They can still tap on buttons, scroll and navigate through
your app, while it handles the heavy loading task in the background.
● The APIs like NSOperation and dispatch queues make it easy to use
concurrency: Creating and managing threads are not easy tasks. Another
important advantage about these APIs is that it helps you achieve
synchronization easily to avoid a race condition. Race condition happens when
multiple threads try to access the shared resource and that leads to unexpected
results. By usage of synchronization, resources are protected from being shared
between multiple threads.

GCD(Grand Central Dispatch)


GCD is the most frequently used API to manage concurrency and execute
operations asynchronously at the kernel unix level of the system. GCD provides and
manages queues of processes.
Dispatch Queues:
It is an easy way to perform tasks asynchronously and parallelly in your
application. Tasks are submitted in form of blocks. There are two types of dispatch
queues: (1) serial queues (2) concurrent queues. All process will run in separate
threads instead of the main thread.
Serial Queues:
This queue can only execute one task at a time. All processes in the same queue
will execute linearly. Tasks can be executed concurrently by using multiple serial
queues.
The advantages of using serial queues:
● Guaranteed serialized access to a shared resource that avoids deadlock.
● Tasks are executed in a linear predicted order. When you submit tasks in a serial
dispatch queue, they will be executed in FIFO(First In First Out) order.
● You can create several numbers of serial queues.
Concurrent Queues:
It allows you to execute multiple processes in parallel. Their execution all occur
concurrently and don’t have to wait for other processes to start. Concurrent queues
begin in the same order but the order of execution cannot be determined, throughput
time or the number of the process being executed at a given point.
● DISPATCH_QUEUE_PRIORITY_HIGH
● DISPATCH_QUEUE_PRIORITY_DEFAULT
● DISPATCH_QUEUE_PRIORITY_LOW
● DISPATCH_QUEUE_PRIORITY_BACKGROUND
These queue types represent the priority of execution.
NSOperation
In iOS SDK, there are two subclasses of NSOperation. The two classes are:
1) NSBlockOperation: This class is used to initiate the operation with one or more
blocks. It can contain more than a block and will be considered as finished when
all blocks are executed.
2) NSInvocationOperation: Use this class to start a process that consists of invoking
a selector on a specified object.
Advantages of NSOperation:
1. It supports dependencies through the method addDependency in the
NSOperation class. When needed to start a process that depends on the
execution of the other NSOperation is used.
2. Secondly, you can change the execution priority by setting the property queue
priority.
3. Tasks with higher priority will be executed first.
4. You can cancel a particular block or all blocks for any given queue. The task can
be cancelled after added to the queue.
5. It has three helpful boolean properties which are finished, cancelled, and ready.
Finished will be set to true once process execution is done. Cancelled is set to
true once the process has been cancelled. Ready is set to true once the process
is about to be executed now.
6. It has an option to set completion block to be called once the task is finished. The
completion block will be called once the boolean property(finished) is set to true
in NSOperation.

Das könnte Ihnen auch gefallen