Stop or completely remove the default iOS dispatch queue

So, I've read several posts about the queuing system, but I can't figure out how to do what I'm looking for. I am currently navigating to the page and loading images using a loop, and each image uses the asynchronous dispatch seen here.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
     //Load Image Code Goes Here
     dispatch_async(dispatch_get_main_queue(), ^{
          //Display Image Code Goes Here After Loading.
     });
});

      

And this works great, however I need to destroy this queue or wait until it is finished before doing anything else. Basically certain pages have dozens and dozens of images, so they all start loading, then I go to a completely separate area of ​​the application and do a completely different image loading (1-2 images) and it takes almost a minute because it is still waiting to load other images. Is there a way to destroy my old queue or pause? I've seen people say "you can, but it will damage the input," which is great because the image will just load on a new page load. Any ideas?

+3


source to share


2 answers


A slightly different approach is to only send a few images to run, and then send another when any of the previous requests ends. This is what the code looks like

- (IBAction)startButtonPressed
{
    self.nextImageNumber = 1;
    for ( int i = 0; i < 2; i++ )
        [self performSelectorOnMainThread:@selector(getImage) withObject:nil waitUntilDone:NO];
}

- (void)getImage
{
    if ( self.view.window && self.nextImageNumber <= 6 )
    {
        int n = self.nextImageNumber++;
        NSLog( @"Requesting image %d", n );

        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://images.apple.com/v/iphone-5s/gallery/a/images/download/photo_%d.jpg", n]];

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
            UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:url]];
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog( @"Received image %d", n );
                [self updateImage:image forView:n];
                [self performSelectorOnMainThread:@selector(getImage) withObject:nil waitUntilDone:NO];
            });
        });
    }
}

      

The uploaded images are named "photo_1.jpg" via "photo_6.jpg". The process starts by requesting the first two photos. When one of these ends ends, the next request is sent and so on, until all 6 photos have been recovered. Key line of code

    if ( self.view.window && self.nextImageNumber <= 6 )

      



The first part if

checks if the view controller stays on the screen. When the user navigates away from the view controller, self.view.window

will be set to nil

. So going from the view controller breaks the download chain.

The second part if

checks if all downloads are complete. This is easy to do because the filenames contain a number. For random filenames, you can populate the NSArray with URLs and then index the array until you reach the end.

I started the chain with 2 uploads because there are only 6 images uploaded in this url. Depending on the size of the image and the number of images, you can start by submitting more. The tradeoff is to maximize bandwidth utilization (starting with more) versus minimal cancellation times (starting with less).

+1


source


if you are using NSURLConnection you have to keep references to them outside of the blocks and then if you move away from the screen [downloadConnection cancel]

on each of them



0


source







All Articles