Algorithm | Concurrent Batch Processing | Swift

Batch processing is basically a processing to process large amount of data in chunks. There are many ways to implement batch processing.

Get Started

Suppose you have 50 objects which is saved in local database and you want to push them on server. Now you have two simple approaches here:

First, push all 50 objects in a single network api request.

Second, split the 50 objects in the multiple batches of x number of objects, and then push batches in serial/concurrent manner.

First approach is very simple and lets start Second approach 👍


First of all, I created a class “BatchManager”. In this, there is a “request” function to process the batch processing.

In the above snapshot, request function has two parameters. One is allObjects array and second is batchSize integer type to define the chunk size. Firstly, Get the total number of batches by the batch size and chunk function from main array.

Here is the array extension to get the chunked sub arrays.

You have all the batches. Now process every batch by network service. I am processing batch requests in concurrent manner so I am taking a DispatchGroup. After the completion of all batches, it notifies to the source function. As per the above request function, you can see, I am processing all the batches in for loop and below that returning the completion closure in the notify block of dispatch group.

Before moving to the defination of pushBatch function. Lets understand the DispatchGroup. As per the Apple, a dispatch group is a group of tasks that you monitor as a single unit.

Groups allow you to aggregate a set of tasks and synchronize behaviors on the group. You attach multiple work items to a group and schedule them for asynchronous execution on the same queue or different queues. When all work items finish executing, the group executes its completion handler. You can also wait synchronously for all tasks in the group to finish executing.

Read this apple documentation for more details.

Now, lets see the defination of pushBatch function.

In the function, first handling the block entering in the dispatch group by enter() and then pushing the batch data to server by the api code of the network service class. When getting the response (success/failure) of api, handling the finishing of block execution in dispatch group by leave() to maintain the count of enter() & leave() of dispatch group. All the batches are starting in serial order but execution is finishing asynchronously. Once the count matches for enter() & leave(), dispatch group calls the notify block.

Now, lets request the data with batch manager class. Below snapshot contains the code to call BatchManager request function.


So this was just a simple approach to push large amount of data to sever in the small packets, to reduce the chances of data loss due to api failure if push large amount of data in single call. Hope you enjoyed it.

Complete Code

Thank You. Happy coding! 👍



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store