kavi kavi - 1 year ago 202
Objective-C Question

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

with examples?

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download