Easy Way to Manage Parallel and Sequential Tasks in Loop For iOS App Development

‘TaskLoop’ is a category of NSArray that is designed to manage asynchronous tasks in a loop. TaskLoop supports sequential task and parallel task. A Task represents an  asynchronous operation.  

Tracking completion of an asynchronous task is not so easy. It is quite difficult when there are multiple asynchronous tasks running. Worst of all, when all tasks are running parallel.

Let’s understand this by an example:

If you want to download images from an array of URLs with following conditions –

  •  Downloading should not block the main thread.
  •  It should track completion of Each download.
  •  It should track completion of ALL downloads.

First Method – A traditional Way

Create one method that will take imageURL as input and a completion block parameter to track completion of each download.

-(void)getImageFromURL:(NSString*)imageURL withCompletion:(void (^)(UIImage *image))completion{

 //download image and call completion block with image

}

Now, we have an array of URLs to call above method from inside ‘for loop’ like…

-(void)startDownloading{

 //arrURLs : array of NSString image URLs

   for (int i=0; i<arrURLs.count; i++) {

    NSString *strImageURL = [arrURLs objectAtIndex:i];

   [self getImageFromURL:strImageURL withCompletion:^(UIImage *image) {

   //completion block

    }];

}

}

In above code, the ‘completion block’ will call upon download of each image. However, it is not giving completion of all tasks. To achieve that we have to compare a number of images downloaded with the total available images.

-(void)startDownloading{

     //arrURLs : array of NSString image URLs

__block NSMutableArray *arrDownloaded = [NSMutableArray new];

   for (int i=0; i<arrURLs.count; i++) {

    NSString *strImageURL = [arrURLs objectAtIndex:i];

   [self getImageFromURL:strImageURL withCompletion:^(UIImage *image) {

        //completion block

        [arrDownloaded addObject:image];

        if (arrDownloaded.count == arrURLs.count) {

            //all image download completed

        }

    }];

}

}

What if images should download in sequence, i.e. one-by-one? We can call download function again with the next URL inside completion block.

After making changes in coding, it will look like this:

-(void)startDownloading{

   //arrURLs : mutable array of NSString image URLs

   NSString *strImageURL = [arrURLs firstObject];

   [self getImageFromURL:strImageURL withCompletion:^(UIImage *image) {

    //completion block

  [arrURLs removeObjectAtIndex:0];

    if (arrURLs.count) {

        //has

        [self startDownloading];

    }else{

        //all completed

    }

}];

}

After implementing this code, we will get the desired result.

Now, let’s use TaskLoop category of NSArray to solve this problem

TaskLoop has two simple category methods for NSArray

1) enumerateTaskParallely that will execute task parallely and give you completion block for all.

-(void)startDownloading{

   //arrURLs : array of NSString image URLs

[arrURLs enumerateTaskParallely:^(id obj, NSUInteger idx, BlockTaskCompletion completion) {

    [self getImageFromURL:obj withCompletion:^(UIImage *image) {

        //this is important to execute next task

        completion(image);

    }];

} blockCompleteAllTask:^{

    //completion for all

}];

}

2) enumerateTaskSequentially that will execute task sequentially and give you completion block for all.

-(void)startDownloading{

   //arrURLs : array of NSString image URLs

NSArray *arrURLs;

[arrURLs enumerateTaskSequentially:^(id obj, NSUInteger idx, BlockTaskCompletion completion) {

    [self getImageFromURL:obj withCompletion:^(UIImage *image) {

        //this is important to execute next task

        completion(image);

    }];

} blockCompleteAllTask:^{

    //completion for all

}];

}

We have used this category in many of our applications like Tuloko and Besana that allows users to manage tasks in loop. Taskloop help to easily manage async tasks.

If you want more information about Taskloop for iOS or want to download this category with an example then click here.    

 

This is a guest post by Sunil Vaishnav.
Author Bio:

Sunil is Sr. iPhone Developer at Space-O Technologies, trusted iPhone app development company in India. Apart from coding, he loves exploring new programming languages and their abilities. Facebook: https://www.facebook.com/sunil.vaishnav.790


MNC vs STARTUP 2

MNC vs STARTUP

 Take your pick 😀

Breaking stereotypes

 

Everyone’s effort counts

 

Seriously! We’re not making fun of TinyOwl.

Bangalore guys can relate to it better. :)

 

Not every Company can boast of a 40% hike.
Boring, lazy Weekends – No not for us. 

 

Facing similar situation? Our team members do 😛

 

Want to build a customised end-to-end Communication platform on your Mobile and Web Apps? We can build it for you at Applozic Inc.  

 

Build real time communication capabilities in your apps? Sign up now: Applozic

Contact: growth@applozic.com

Note: All images are property of Applozic Inc, you can use them as long as you provide a reference of this blog post.