10

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?

kavi
  • 143
  • 1
  • 1
  • 9
  • 1
    Requests for tutorials and other off-site resources is off-topic here. To save your question from closing, I have edited out the off-topic demands from your question. You can always rollback if you want, but If you do, community will have to close it – NSNoob Oct 10 '16 at 07:51

2 Answers2

9

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

Community
  • 1
  • 1
Jamshed Alam
  • 12,424
  • 5
  • 26
  • 49
  • Ok. i modified it. Thank. Please, cancel if you downvoted it please. @NSNoob – Jamshed Alam Oct 10 '16 at 08:04
  • Thanks Jamshed Alam , Actually i am saving image in nsdocument directory path in sigleton class ,where i have to place the code to save the dictionary .Where i have to place these two code in my singleton class? – kavi Oct 10 '16 at 08:19
  • Apply one NSOperationQueue for each image. So the code will be in a loop. Example: for (;;) { NSOperationQueue *op...... whole code... } . Try it.. hope you can do it. If you can not do it, please paste some codes. – Jamshed Alam Oct 10 '16 at 08:26
  • i don't know who down vote it. Hello somebody, please explain in the comment section for why you have down vote it !! . – Jamshed Alam Oct 10 '16 at 08:28
  • Thanks Jamshed Alam – kavi Oct 10 '16 at 10:32
  • Please, upvote it. Someone downvote it. i dont know why. :( @kavi – Jamshed Alam Oct 10 '16 at 10:39
  • @JamshedAlam It isn't my DV mate. The upvote however is mine :) – NSNoob Oct 18 '16 at 12:40
1

Swift3 Create an operation queue

lazy var imgSaveQueue: OperationQueue = {
    var queue = OperationQueue()
    queue.name = "Image Save Queue"
    queue.maxConcurrentOperationCount = 1
    return queue
}()

Add operation to it

imgSaveQueue.addOperation(BlockOperation(block: { 
       //your image saving code here 
    }))

For Objective C:

[[NSOperationQueue new] addOperationWithBlock:^{ 

      //code here 

}];
NSNoob
  • 5,548
  • 6
  • 41
  • 54
Hunaid Hassan
  • 732
  • 5
  • 13