Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use NSOperation & NSOperationQueue in Objective-C?

I am developing a custom camera application.What I am doing is , I am taking picture using camera and showing them in same VC bottom of the screen.

I am storing the image in local dictionaries and NSDocument directory path. If the picture is in local dictionaries it will take from local dictionary else it will take from the NSDocument directory path.

After memory warning received, I just nil the dictionary ,so it will take the images from the NSDocument directory path.

Using both will showing images in slow process.My UI got not that much good in showing images.

So I want to store the images in NSDocument directory path using NSOperation.

I don't have much knowledge about NSOperation. I have searched in google , I am just getting swift tutorials while I require help in Objective C.

So please can anyone explain the NSOperation and NSOperationQueue with examples?

like image 409
kavi Avatar asked Oct 10 '16 06:10

kavi


People also ask

What are the benefits of NSOperation?

The NSOperationQueue also adds a number of benefits to the mix. For example, you can specify the maximum number of queued operations that can run simultaneously. This makes it easy to control how many operations run at the same time or to create a serial operation queue.

What is NSOperation in Swift?

An abstract class that represents the code and data associated with a single task.

How do you use an operation queue?

An operation queue invokes its queued Operation objects based on their priority and readiness. After you add an operation to a queue, it remains in the queue until the operation finishes its task. You can't directly remove an operation from a queue after you add it.

Is NSOperation thread safe?

You can safely use a single NSOperationQueue object from multiple threads without creating additional locks to synchronize access to that object. Operation queues use the Dispatch framework to initiate the execution of their operations.


1 Answers

Apply this for each work :

        // Allocated here for succinctness.
        NSOperationQueue *q = [[NSOperationQueue alloc] init];

        /* Data to process */
        NSData *data = [@"Hello, I'm a Block!" dataUsingEncoding: NSUTF8StringEncoding];

        /* Push an expensive computation to the operation queue, and then
         * display the response to the user on the main thread. */
        [q addOperationWithBlock: ^{
            /* Perform expensive processing with data on our background thread */
            NSString *string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];



            /* Inform the user of the result on the main thread, where it's safe to play with the UI. */

            /* We don't need to hold a string reference anymore */

        }];

And you can also apply without NSOperationQueue :

       dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            // Your Background work

            dispatch_async(dispatch_get_main_queue(), ^{
                // Update your UI


            });
        });

Try these more :

  1. NSOperationQueue addOperationWithBlock return to mainQueue order of operations

  2. http://landonf.org/code/iphone/Using_Blocks_1.20090704.html

  3. https://videos.raywenderlich.com/courses/introducing-concurrency/lessons/7

like image 133
Jamshed Alam Avatar answered Nov 15 '22 07:11

Jamshed Alam