1

I am implementing the contact module basically adding,deleting,searching and listing the contacts.

Here i used file to persist the data like storing all the contacts in file(json format) and deserializing back to the object.

Now my target is to perform serialization and deserialization functions in background thread using nsoperation.And how one class extends nsopertions and what to do in that class.

I am new to mac os.And i cant understand what exactly nsoperation means?how to use it in my module.how to make them run concurrently.I had seen lot of tutorials but still it is very clumsy for me.I am really in need of help.Thanks in advance.

user2017
  • 73
  • 7
  • 3
    Did you read Apple's documentation? What did you not understand? Did you also read about GCD? Watched the WWDC videos? – gnasher729 Jan 29 '17 at 15:24

1 Answers1

4

We have lot of answer to your question

What is NSOperation?

First Apple Reference Says

The NSOperation class is an abstract class you use to encapsulate the code and data associated with a single task. Because it is abstract, you do not use this class directly but instead subclass or use one of the system-defined subclasses (NSInvocationOperation or BlockOperation) to perform the actual task. Despite being abstract, the base implementation of NSOperation does include significant logic to coordinate the safe execution of your task. The presence of this built-in logic allows you to focus on the actual implementation of your task, rather than on the glue code needed to ensure it works correctly with other system objects.

Then Simple meaning of NSOperation

NSOperation represents a single unit of work. It’s an abstract class that offers a useful, thread-safe structure for modeling state, priority, dependencies, and management.

Do you need to run it concurrently?

What is Concurrency?

Doing multiple things at the same time.

Taking advantage of number of cores available in multicore CPUs.

Running multiple programs in parallel.

Why NSOperationQueue?

For situations where NSOperation doesn’t make sense to build out a custom NSOperation subclass, Foundation provides the concrete implementations NSBlockOperation and NSInvocationOperation.

Examples of tasks that lend themselves well to NSOperation include network requests, image resizing, text processing, or any other repeatable, structured, long-running task that produces associated state or data.

But simply wrapping computation into an object doesn’t do much without a little oversight. That’s where NSOperationQueue comes in

What is NSOperationQueue?

NSOperationQueue regulates the concurrent execution of operations. It acts as a priority queue, such that operations are executed in a roughly First-In-First-Out manner, with higher-priority (NSOperation.queuePriority) ones getting to jump ahead of lower-priority ones. NSOperationQueue can also limit the maximum number of concurrent operations to be executed at any given moment, using the maxConcurrentOperationCount property.

NSOperationQueue itself is backed by a Grand Central Dispatch queue, though that’s a private implementation detail.

To kick off an NSOperation, either call start, or add it to an NSOperationQueue, to have it start once it reaches the front of the queue. Since so much of the benefit of NSOperation is derived from NSOperationQueue, it’s almost always preferable to add an operation to a queue rather than invoke start directly.

Also

Operation queues usually provide the threads used to run their operations. In OS X v10.6 and later, operation queues use the libdispatch library (also known as Grand Central Dispatch) to initiate the execution of their operations. As a result, operations are always executed on a separate thread, regardless of whether they are designated as concurrent or non-concurrent operations

So your code should be

 NSOperationQueue *backgroundQueue = [[NSOperationQueue alloc] init];
 [backgroundQueue addOperationWithBlock:^{

    //Your Background Work kHere
    .....

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{

      //Your Main Thread(UI) Work Here
      ....
    }];
 }];
Community
  • 1
  • 1
user3182143
  • 9,459
  • 3
  • 32
  • 39