Latest 0.3.0
Homepage https://github.com/ivanmoskalev/Chop
License MIT
Platforms ios 8.0, requires ARC
Frameworks Foundation
Authors

Write synchronous, testable core easily with Chop.

CI Status
codecov.io
Version
License
Platform

What is Chop?

Chop is a Swift microframework providing a simple implementations of async tasks and task groups.

  • Tasks are abstractions over asynchronous processes that can yield several values during their lifetime (think of progressive images or cached-then-remote values). They can also finish with an error. Tasks are lazy – which means they are not started before they really need to. Furthermore, tasks are cancelled when they are no longer referenced (ie, deallocated), freeing up jobs and resources they are associated with.
  • Task Groups are execution contexts for tasks. They allow to place uniqueness constraints on tasks and apply a variety of behaviors around that (for example, replacing existing task with the same unique ID with the new one or ignoring the latter). Task Groups also free up all tasks they manage when they are deallocated, making them a nice way to particular tasks to, say, user interface.

Here is a small example of how Chop can be used to execute a task that progressively loads a collection of items (for example, first fetching the locally cached value, and then the remote value). Only one task is allowed to execute; subsequent similar tasks are ignored until the first one finishes:

// The context in which the tasks can execute. 
// `policy` describes what should be done if a task with an identical `taskId` is added to group.
// In this case we want to ignore subsequent tasks with the same `taskId` until the first is completed.
// Other options exist.
let taskGroup = TaskGroup(policy: .Ignore)

self.isLoading = true
self.interactor
    .fetchIssues(request, itemLimit: 10, progressive: true) // API that exposes a Chop'esque interface.
    .onUpdate { [weak self] in
      // Can be executed multiple times (ie., providing first cached items, then remote items).
      self.items = $0
    }
    .onFailure { [weak self] in
      // Executed only once if error occurs.
      self.handleError($0)  
    }
    .onCompletion { [weak self] in
      // Executed when the task has finished, regardless of result.
      self.isLoading = false 
    }
    // `taskId` is optional. If provided, task will be uniqued based on this id.
    .registerIn(self.taskGroup, taskId: "itemFetch")

Imagine how much boilerplate code would you have to write to acheive this behavior. With Chop it is easy, declarative and rather enjoyable.

The task performs work only as long as it is referenced by a TaskGroup. Consequently, if the TaskGroup is destroyed, all managed tasks are interrupted and you don’t have to care about them anymore.

Requirements

Chop requires Swift 2.0.

Installation

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

pod "Chop"

Author

Ivan Moskalev, [email protected]

License

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

Latest podspec

{
    "name": "Chop",
    "version": "0.3.0",
    "summary": "Expressive RAII abstraction over async operations.",
    "description": "Chop is a delightful framework that provides a simple and expressive way to manage async operations.",
    "homepage": "https://github.com/ivanmoskalev/Chop",
    "license": "MIT",
    "authors": {
        "Ivan Moskalev": "[email protected]"
    },
    "source": {
        "git": "https://github.com/ivanmoskalev/Chop.git",
        "tag": "0.3.0"
    },
    "social_media_url": "https://twitter.com/ivanmoskalev",
    "platforms": {
        "ios": "8.0"
    },
    "requires_arc": true,
    "source_files": "Source/**/*",
    "frameworks": "Foundation",
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This