• Stars
    star
    113
  • Rank 310,115 (Top 7 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created about 12 years ago
  • Updated almost 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Concurrency control flow system for Objective-C

IIIAsync is a control flow system for managing multiple operations serially or in parallel. It can collect the results of multiple operations, either objects or errors. When all operations finish, you get a completion handler with the results in a structured order, regardless of which order they complete. You can run these operations either on the main queue, on a single background thread pool, or on a multi-threaded background pool. It makes it easier to organize code and obtain results, letting you build powerful higher-order operations that rely on multiple asynchronous calls.

IIIAsync is heavily modeled after and inspired by the brilliant async.js library by Caolan McMahon.

Installation

Download the code (either standalone or as a submodule) and include IIIAsync.h and IIIAsync.m in your Xcode project.

Usage

There are three IIIAsync singletons which determine which context to run operations on. Either:

  • [IIIAsync mainThread],
  • [IIIAsync backgroundThreadAsync], or
  • [IIIAsync globalAsync]

These run your code in a dispatch queue on the main thread, a single background thread, or the global background thread, respectively.

Each of these has the following set of APIs:

  • -[IIIAsync iterateSerially:withIteratorTask:completionHandler:] - iterates over an array, invoking the iterator task on each item in the array, one at a time, and performs the completion handler after the iteration is finished. Order is guaranteed; each item will not be invoked until the previous item is completed. The result will be an array of return values and an optional error.
  • -[IIIAsync iterateParallel:withIteratorTask:completionHandler:] - iterates over an array, invoking the iterator task on each item in the array, and performs the completion handler after the iteration is finished. Order is not guaranteed. The result will be an array of return values and an optional error.
  • -[IIIAsync runTasksInSeries:withCompletionHandler:] - iterates over an array of tasks serially and performs the completion handler when the iteration is complete. Order is guaranteed; each task will not be invoked until the previous task is completed. The result will be an array of return values and an optional error.
  • -[IIIAsync runTasksInParallel:withCompletionHandler:] - iterates over an array of tasks in parallel and performs the completion handler when the iteration is complete. Order is not guaranteed. The result will be an array of return values and an optional error.
  • -[IIIAsync runWhileTrue:performTask:withCompletionHandler:] - Performs a task while a conditional returns true, and performs the completion handler when the conditional returns false. Returns nothing and an optional error.
  • -[IIIAsync runWhileFalse:performTask:withCompletionHandler:] - Performs a task while a conditional returns false, and performs the completion handler when the conditional returns true. Returns nothing and an optional error.

There are a few block types that can be called, based on API:

  • IIIAsyncTask: Accepts an IIIAsyncTaskCompletionHandler block (see below). You supply an array of these to the run*: APIs for your tasks. When the task is complete, you call the IIIAsyncTaskCompletionHandler block to signal that it has finished.
  • IIIAsyncIteratorTask: Accepts an id object, an NSUInteger representing the index of the object, and an IIIAsyncTaskCompletionHandler block (see below). You supply one of these to the iterate*: APIs for your operations. When each iteration has completed, you call the IIIAsyncTaskCompletionHandler block to signal that it has finished.
  • IIIAsyncTaskCompletionHandler: Accepts an id object and an optional error. You call this with the result of your operation and an optional NSError, which will be collected as necessary. You also implement this to get the results of an async operation.
  • IIIAsyncConditional: Accepts no parameters and returns a BOOL. You implement this for the runWhile*: APIs, to determine when to break out of the loop.

Example

For our purposes we will assume there is a method that looks like this:

-(void)loadImageAtURL:(NSURL *)url handler:(void (^)(UIImage *image, NSError *error))handler;

Load three images in parallel:

NSArray *images = @[
	[NSURL URLWithString:@"http://placekitten.com/200/300"],
	[NSURL URLWithString:@"http://placekitten.com/300/200"],
	[NSURL URLWithString:@"http://placekitten.com/450/450"]
];

[[IIIAsync mainThread] iterateParallel:images withIteratorTask:^(id object, NSUInteger index, IIIAsyncTaskCompletionHandler completionHandler){
	NSURL *url = (NSURL *)object;
	[self loadImageAtURL:url handler:^(UIImage *image, NSError *error){
		completionHandler(image, error);
	}];
} completionHandler:^(id result, NSError *error){
	if(error){
		NSLog(@"One of the operations returned an error: %@", error);
	}else{
		NSArray *images = (NSArray *)result;
		NSLog(@"All three images: %@", result);
	}
}];

Tests

The Xcode project includes a unit test suite. You can run it by opening the Xcode project, selecting the IIIAsync scheme, and selecting "Test" from the Product menu.

No pull requests will be considered unless they contain adequate, passing unit tests.

License

tl;dr: MIT license, do what you want, just attribute for it

Copyright (c) 2012 Steve Streza

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

Relayout

Swift microframework for declaring Auto Layout constraints functionally
Swift
555
star
2

Barista

A modular, embeddable web server for Objective-C.
Objective-C
500
star
3

XPCKit

XPC simplified for Cocoa. Deal with NS* objects instead of xpc_object_t.
Objective-C
251
star
4

DerpKit

Objective-C categories and subclasses of things that should be in Foundation and other frameworks
Objective-C
137
star
5

DevCenter.me

Directory of developer center websites with memorable URL shortcuts
CSS
80
star
6

awesome-ios-widgets

A curated list of home screen widgets for apps on iOS 14+
74
star
7

VillainousStyle

A standalone Mac/iPhone port of the TTStyle and TTShape classes from the Three20 project
Objective-C
70
star
8

EasyTweet

A simple Objective-C wrapper for the Twitter API with no OAuth dependencies
Objective-C
60
star
9

CrashReporter

Send iOS crash reports by email
C
57
star
10

url-shrink

URL Shrink provides a system-wide utility to shorten and expand URLs across multiple services (is.gd, bit.ly, etc.)
Objective-C
47
star
11

sscore

Cocoa code I use just about everywhere.
Objective-C
36
star
12

better-xcode-templates

A package of better Xcode templates for iPhone and Mac
Objective-C
34
star
13

CoreFoundation

A git-based mirror of the official CoreFoundation code
C
29
star
14

Swearch

An iOS-optimized web app for accessing search engines
JavaScript
29
star
15

PlayerPiano

A Pianobar GUI for Mac OS X Snow Leopard
C
29
star
16

oauthery

Tool for manually logging into Twitter using OAuth, with Cocoa code demonstrating the OAuth login process for Twitter
Objective-C
27
star
17

MWNetworking

Right now, one Objective-C class to download a thing. Eventually, DOWNLOAD ALL THE THINGS.
Objective-C
23
star
18

BlockKit

YOU get a block, YOU get a block, EVERYONE gets a block!
Objective-C
20
star
19

caboose

A Mac client for getting push notifications from the Boxcar service
C
16
star
20

TOML

Kind-of-works TOML parser for Objective-C
Objective-C
12
star
21

AppApp

AppApp is an iOS app for http://app.net. It was hastily copy pasted together at 3am.
Objective-C
11
star
22

TDAppKit

Git mirror of TDAppKit from @itod
Objective-C
11
star
23

EmbedMonster

Universal (eventually) audio/video controller JavaScript API for embeddable media
JavaScript
9
star
24

MiniPiano

An iPhone app which plays music from Pandora
Objective-C
7
star
25

html5playlist

A simple web app to play a playlist
JavaScript
7
star
26

node-pop3

A POP3 library for node.js. Maybe it'll work one day.
6
star
27

technicolor

Media organization tool for Mac OS X
Objective-C
6
star
28

fightclub

Cocoa framework for connecting to Hellanzb servers
Objective-C
6
star
29

Colloquy

Git port of the Colloquy IRC client for Mac
Objective-C
6
star
30

hal-project

Home automation project written in Python for Arduino boards (SVN mirror)
Python
6
star
31

lessn-service

OS X Service for shortening a URL using your installation of Lessn/ButteredURLs
Objective-C
6
star
32

ZNC-Node

A module for ZNC to add node.js-based modules
C++
4
star
33

flash-machine

Cocoa application to host SWF files and bookmarks to SWFs
Objective-C
4
star
34

technicolor-networking

A set of networking plugins for Technicolor, including a shared HTTP server
Objective-C
3
star
35

bmo

Plugin-based XMPP bot
JavaScript
3
star
36

NudeWiki

A wiki, built with Node.js and MongoDB, because that's what you build when learning new server-side technologies
JavaScript
2
star
37

facebook-iphone-sdk

Facebook Connect for iPhone
Objective-C
2
star
38

dock-killer

OS X app for killing the dock via the menu bar
Objective-C
2
star
39

technicolor-file-importer

A simple plugin for Technicolor to demonstrate importing files into records
Objective-C
2
star
40

StatsKit

An open source analytics system for developers on any platform. In development.
JavaScript
2
star
41

turntable.userscripts.fm

Various userscripts for turntable.fm
JavaScript
2
star
42

csh-drink-iphone

iPhone client for the CSH Drink machines
2
star
43

node-pq

A Redis-backed priority queue for node.js.
JavaScript
2
star
44

dotfiles

Various dotfiles
Shell
2
star
45

technicolor-fuse

A FUSE plugin for accessing files stored in Technicolor
Objective-C
2
star
46

Boxque

A node.js-based server framework for queueing Boxcar notifications through Resque
JavaScript
2
star
47

node-boxcar

A node.js Connector for Boxcar
1
star
48

bitlbee

Mirror of the Bitlbee project, with some of my own changes
C
1
star
49

InformalProtocolTheme

Theme for InformalProtocol.com
JavaScript
1
star
50

CoreMustache

C
1
star
51

js-calendar-picker

A small calendar picker written with jQuery which reveals itself as needed.
1
star