Latest 0.1.5
License MIT
Platforms ios 8.0
Frameworks Foundation

Build Status

What is MBTaskContainer?

MBTaskContainer is a simple class that lets you add your NSURLSessionTasks into it and read active tasks from it safely.

Example scenario

Let’s say your server use OAuth2 authentication standard, so you need to obtain a token before establishing a connection to access your API
And again, let’s say your application has an architecture like the following:

ViewController -> DataController where

  1. ViewController
    • Typical UIViewController subclass
    • it may have an UIButton to cancel ongoing network operations
  2. DataController
    • NSObject subclass
    • responsible for making network calls

In that case, we cannot return actual ongoing network tasks to ViewController from DataController immediately as even a simple network request would be like the following:

  1. ViewController calls DataController‘s getItems method
  2. DataController makes the request
  3. Request returns 401 Unauthorized responsible
  4. DataController makes another request to obtain a new token
  5. After receiving new token, DataController retries getItems request

In short, there will be many request that are done asynchronously and some follow the others.
In order to address this problem, you can use MBTaskContainer!

Example solution

  1. Returns an MBTaskContainer instance from DataController:getItems.
  2. Whenever a new network task is created, add it to returned instance.
  3. ViewController can access active network tasks at any time.

Why can’t we do that with NSArray?

  1. MBTaskContainer lets you add/read tasks from multiple threads in a safe way.
    • Multiple readers and single writer/remover at a time.
  2. MBTaskContainer takes care of removal. You don’t need to remove completed tasks.

Example Usage

// DataController.m
- (MBTaskContainer *)seriallyGetRepositoriesOrganizationsMembers {
    MBTaskContainer *taskContainer = [MBTaskContainer new];

    NSURLSessionTask *repositories = [self fetchDataWithRelativeURL:repositoriesURL completion:^(NSError *error) {
        if (error == nil) {
            NSURLSessionTask *organizations = nil;
            organizations = [self fetchDataWithRelativeURL:organizationsURL completion:^(NSError *error) {
                if (error == nil) {
                    NSURLSessionTask *members = nil;
                    members = [self fetchDataWithRelativeURL:membersURL completion:nil];

                    [taskContainer addTask:members];

            [taskContainer addTask:organizations];

    [taskContainer addTask:repositories];

    return taskContainer;

// ViewController.m
self.serialTaskContainer = [self.dataController seriallyGetRepositoriesOrganizationsMembers];
NSArray *activeTasks = [self.serialTaskContainer getTasks];
// To cancel ongoing and potential new tasks
self.serialTaskContainer.state = MBTaskContainerStateCancelling;


MBTaskContainer is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "MBTaskContainer"


Mert Buran, [email protected]


MBTaskContainer is available under the MIT license. See the LICENSE file for more info.

Latest podspec

    "name": "MBTaskContainer",
    "version": "0.1.5",
    "summary": "Multi-threaded NSURLSessionTask container",
    "description": "You have a good separation of UI and data-network but you make multiple network requests at data layer and you are having troubles with notifying UI layer about that? You dont want to have extra notifying parameter in your data layer classes and you dont wanna deal with blocks-delegate methods? You dont wanna use ReactiveCocoa since it is huge and a totally different paradigm? Then you are at the right place! MBTaskContainer is for you! Return it to UI layer from data layer and then you can modify if from any thread and layer! UI layer can query active tasks with safety whenever it is needed.",
    "homepage": "",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    "authors": {
        "Mert Buran": "[email protected]"
    "source": {
        "git": "",
        "tag": "0.1.5"
    "social_media_url": "",
    "platforms": {
        "ios": "8.0"
    "source_files": [
    "public_header_files": "MBTaskContainer/Classes/{MBTaskContainer,MBTaskContainerDelegate}.h",
    "frameworks": "Foundation"

Pin It on Pinterest

Share This