iphonensoperationnsoperationqueuealassetslibrarynsblockoperation

NSBlockOperation or NSOperation with ALAsset Block to display photo-library images using ALAsset URL


I am asking this question regarding my questions Display photolibrary images in an effectual way iPhone and Highly efficient UITableView "cellForRowIndexPath" method to bind the PhotoLibrary images.

So I would like to request that answers are not duplicated to this one without reading the below details :)

Let's come to the issue,

I have researched detailed about my above mentioned issue, and I have found the document about operation queues from here.

So I have created one sample application to display seven photo-library images using operation queues through ALAsset blocks.

Here are the sample application details.

Step 1:

In the NSOperationalQueueViewController viewDidLoad method, I have retrieved all the photo-gallery ALAsset URLs in to an array named urlArray.

Step 2:

After all the URLs are added to the urlArray, the if(group != nil) condition will be false in assetGroupEnumerator, so I have created a NSOperationQueue, and then created seven UIImageView's through a for loop and created my NSOperation subclass object with the corresponding image-view and URL for each one and added them in to the NSOperationQueue.

See my NSOperation subclass here.

See my implementation (VierwController) class here.

Let's come to the issue.

It not displaying all the seven images consistently. Some of the images are missing. The missing order is changing multiple times (one time it doesn't display the sixth and seventh, and another time it doesn't display only the second and third). The console log displays Could not find photo pic number. However, the URLs are logged properly.

You can see the log details here.

Are there any mistakes in my classes?

Also, when I go through the above mentioned operational queue documentation, I have read about NSBlockOperation. Do I need to implement NSBlockOperation instead of NSOperation while dealing with ALAsset blocks?

The NSBlockOperation description says

A class you use as-is to execute one or more block objects concurrently. Because it can execute more than one block, a block operation object operates using a group semantic; only when all of the associated blocks have finished executing is the operation itself considered finished.

How can I implement the NSBlockOperation with ALAsset block regarding my sample application?

I have gone through Stack Overflow question Learning NSBlockOperation. However, I didn't get any idea to implement the NSBlockOperation with ALAsset block!!


Solution

  • You have a line in your DisplayImages NSOperation subclass where you update the UI (DisplayImages.m line 54):

    self.imageView.image = topicImage;
    

    This operation queue is running on a background thread, and we know that you should only update the state of the UI on the main thread. Since updating the view of an image view is definitely updating the UI, this can be simply fixed by wrapping the call with:

    dispatch_async(dispatch_get_main_queue(), ^{
       self.imageView.image = topicImage;
    });
    

    This puts an asynchronous call on the main queue to update the UIImageView with the image. It's asynchronous so your other tasks can be scheduled in the background, and it's safe as it is running on the main queue - which is the main thread.