Latest 0.1.0
Homepage https://github.com/michaeldai1006/TDOuroboros
License MIT
Platforms ios 8.0
Authors

TDOuroboros

Version
License
Platform

TDOuroboros is an async task chaining library written in Swift.

Why TDOuroboros

TDOuroboros is an iOS library written in Swift which provides the ability to chain async tasks, such as animation tasks, HTTP request tasks, etc.
Traditionally, connect async tasks can be done by nesting completion handlers, such as:

import UIKit

UIView.animate(withDuration: 1.0, animations: {
    // Perform animation 1
}) { (result1) in
    UIView.animate(withDuration: 1.0, animations: {
        // Perform animation 2
    }, completion: { (result2) in
        UIView.animate(withDuration: 1.0, animations: {
            // Perform animation 3
        }, completion: { (result3) in
            print("Animation 1-3 executed");
        })
    })
}

There are 2 major defects about this approach:

  1. Pyramid of doom
  2. Unable to handle tasks generated at run time

TDOuroboros handles tasks very differently:
A task queue shall be created at first, to keep and manage async tasks. Every time a new task is generated or defined, it shall be enqueued into the task queue created at the very beginning. After the completion of each task, it shall notify the task queue about the task has just been completed. The task queue will dequeue a new task and execute it.

As an example:
A demo iOS project is included in this repo, under subfolder Example.
Every time the list of shapes is reordered by drag and drop, animation tasks of each shape will be enqueued into the shared task queue instance, in the order of from left to right on the list. After that the animation for each shape will be executed in order.

TDOuroboros

Usage

Use shared task queue instance

If only one task queue is needed for the App or the Framework you are creating, or if you wish to have one shared task queue which can be used anywhere in the Application. You can use the shared instance of task queue manager.

import TDOuroboros
import UIKit

// Enqueue a new task into the shared task queue manager instance
TDTaskQueueManager.shared.enqueue { [weak self] in

    // Task code here
    UIView.animate(withDuration: 1.0, animations: {
        print("Hello World")
    }, completion: { (result) in
        if (result) {
            // After task finished, notify task queue manager about task completed
            TDTaskQueueManager.shared.taskCompleted()
        }
    })

}

Create your own task queue

You can always create your own task queue manager instance, many queue instances can coexist in one App/Framework.

import TDOuroboros
import UIKit

// Create new task queue manager instance
let taskQueue = TDTaskQueueManager()

// Enqueue a new task into the queue
taskQueue.enqueue { [weak self] in

    // Task code here
    UIView.animate(withDuration: 1.0, animations: {
        print("Hello World")
    }, completion: { (result) in
        if (result) {
            // After task finished, notify task queue manager about task completed
            self?.taskQueue.taskCompleted()
        }
    })

}

Installation

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

pod 'TDOuroboros'

About

We love PRs, come become part of the project!

License

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

Latest podspec

{
    "name": "TDOuroboros",
    "version": "0.1.0",
    "summary": "A Swift async library.",
    "description": "This CocoaPod provides the ability to chain async tasks.",
    "homepage": "https://github.com/michaeldai1006/TDOuroboros",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "michaeldai1006": "[email protected]"
    },
    "source": {
        "git": "https://github.com/michaeldai1006/TDOuroboros.git",
        "tag": "0.1.0"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "*.{h,m,swift}"
}

Pin It on Pinterest

Share This