AlignMinds Technologies logo

How to Handle NSOperation in Your Mobile App?

MODIFIED ON: May 17, 2024 / ALIGNMINDS TECHNOLOGIES / 0 COMMENTS

Each day at our work consists of a sequence of tasks that fill our working hours. In the same way, when you create an application, all the interface components (table views, UI controls, alerts, etc.) are run inside the main thread of your application. At some point in your application, you will want to populate these views with data. This data can be retrieved from the disk, the web, a database, etc. Using NSOperation, you can manage these data retrieval tasks efficiently without blocking the main thread.

The issue that we face when we want to populate or handle a huge amount of data is

‘How would you efficiently load this data into your application interface while still allowing the user to have control of the application without any disturbance?’

Many applications in the app store simply ‘freeze’ while their application data is loaded. This will disappoint the user interaction. The secret to making apps without this problem is to move the unnecessary work (the activities which can take place without user interaction) to the background as possible.

The iOS developer has two options here.

  • Grand Central Dispatch
  • NSOperation

Let me explain about NSOperation.

NSOperationQueue

NSOperationQueue manages the concurrent execution of code operations in Xcode. It acts as a priority queue because operations are executed in a First-In-First-Out manner, with higher-priority (NSOperation.queuePriority) ones getting to the lower-priority ones.

NSOperation

NSOperation is a single unit of work. It’s an abstract class that provides a useful, thread-safe structure for programming.NSOperation will perform network requests, text processing, or any other repeatable long-running task that produces associated state or data.

There are two different operations you can create, which are prebuilt in once which are NSInvocationOperation and NSBlockOperation.

Priority

All operations may not be equally important. Setting the queuePriority property will promote or defer an operation in an NSOperationQueue according to the following rankings:

NSOperationQueuePriority

typedef enum : NSInteger { NSOperationQueuePriorityVeryLow = -8, NSOperationQueuePriorityLow = -4, NSOperationQueuePriorityNormal = 0, NSOperationQueuePriorityHigh = 4, NSOperationQueuePriorityVeryHigh = 8 } NSOperationQueuePriority;

The following enumerated values are used to denote the priority of operation. Operations are considered based on priority.

NSQualityOfService

typedef enum : NSInteger { NSQualityOfServiceUserInteractive = 0×21, NSQualityOfServiceUserInitiated = 0×19, NSQualityOfServiceUtility = 0×11, NSQualityOfServiceBackground = 0×09, NSQualityOfServiceDefault = -1 } NSQualityOfService;

Implementation

NSBlockOperation always executes a block. NSInvocationOperation executes an NSInvocation ( a method defined by selector, target or object).

NSInvocationOperation

NSOperationQueue *myQueue = [[NSOperationQueue alloc] init]; NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(printNumbers) object:@”operation1″]; operation1.queuePriority = NSOperationQueuePriorityLow; operation1.qualityOfService = NSOperationQualityOfServiceBackground; [myQueue addOperation:operation1]; NSInvocationOperation *operation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(PrintAlphabets) object:@”operation2″]; operation2.queuePriority = NSOperationQueuePriorityHigh; operation2.qualityOfService = NSOperationQualityOfServiceBackground; [myQueue addOperation:operation2]; -(void)printNumbers { for (int i = 0; i<10; i++) { NSLog(@”%d”,i); } } -(void)PrintAlphabets { for (char a = ‘a’; a <= ‘z’; a++) { NSLog(@”%c”,a); } }

Output

a

0

b

1

c

2

d

3

.

.

NSBlockOperation

NSOperationQueue *myQueue = [[NSOperationQueue alloc] init]; NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^(void){ [self PrintAlphabets]; }]; operation1.queuePriority = NSOperationQueuePriorityLow; operation1.qualityOfService = NSOperationQualityOfServiceBackground; [myQueue addOperation:operation1]; NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^(void){ [self printNumbers]; }]; operation2.queuePriority = NSOperationQueuePriorityHigh; operation2.qualityOfService = NSOperationQualityOfServiceBackground; [myQueue addOperation:operation2];

Output

a

0

b

1

c

2

d

3

.

.

CompletionBlock

When an NSOperation completes, it will execute its completionBlock only once. This provides a way to customize the behaviour of operation when used in a model or view controller.

NSOperation *operation = …; operation.completionBlock = ^{ NSLog(“Completed”); }; [[NSOperationQueue mainQueue] addOperation:operation];

I hope the above tips shared based on my experience help you when you need to handle NSOperation in your next project.

Happy Coding!

Bibin Binny Mathew

Leave a reply

Your email address will not be published.

0 0 votes
Article Rating
guest
0 Comments
Inline Feedbacks
View all comments