GCD - Set max concurrent operations for a queue

Discussion in 'iOS Programming' started by dantastic, Nov 13, 2013.

  1. dantastic macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #1
    I'm currently using a dispatch queue to concurrently process and save images taken by the camera in my app.

    I need to be able to limit the number of concurrent operations or the app can run out of memory. I know I can set max number of concurrent operations for a NSOperationQueue but I have not been able to find any info regarding dispatch blocks...

    Code:
    // the queue I need to limit
    imageProcessingQueue = dispatch_queue_create("com.app.imageProcessingQueue", DISPATCH_QUEUE_CONCURRENT);
    
    
    - (void) saveImage:(UIImage *)image {
    	
    	dispatch_async(imageProcessingQueue, ^{
    		
    		// Process the image and save to disk.
    		
    		dispatch_async(dispatch_get_main_queue(), ^{
    			
    			// Save the image path and other image info to core data.
    		});
    	});
    }
     
  2. Mark FX macrumors regular

    Mark FX

    Joined:
    Nov 18, 2011
    Location:
    West Sussex, UK
  3. dantastic thread starter macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #3
    Thanks for that but in this particular instance there's another answer.
    If you need a NSOperationQueue, use a NSOperationQueue.

    By using NSOperationQueue's addOperationWithBlock I was able to use the existing code pretty much and it works great :)
     

Share This Page