• Stars
    star
    268
  • Rank 153,144 (Top 4 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created over 12 years ago
  • Updated over 7 years ago

Reviews

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

Repository Details

A persistent background job queue for iOS.

Queue

A persistent background job queue for iOS.

While NSOperation and NSOperationQueue work well for some repetitive problems and NSInvocation for others, iOS doesn't really include a set of tools for managing large collections of arbitrary background tasks easily. EDQueue provides a high-level interface for implementing a threaded job queue using GCD and SQLLite3. All you need to do is handle the jobs within the provided delegate method and EDQueue handles the rest.

Getting Started

The easiest way to get going with EDQueue is to take a look at the included example application. The Xcode project file can be found in Project > queue.xcodeproj.

Setup

EDQueue needs both libsqlite3.0.dylib and FMDB for the storage engine. As always, the quickest way to take care of all those details is to use CocoaPods. EDQueue is implemented as a singleton as to allow jobs to be created from anywhere throughout an application. However, tasks are all processed through a single delegate method and thus it often makes the most sense to setup EDQueue within the application delegate:

YourAppDelegate.h

#import "EDQueue.h"
@interface YourAppDelegate : UIResponder <UIApplicationDelegate, EDQueueDelegate>

YourAppDelegate.m

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [[EDQueue sharedInstance] setDelegate:self];
    [[EDQueue sharedInstance] start];
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    [[EDQueue sharedInstance] stop];
}

- (EDQueueResult)queue:(EDQueue *)queue processJob:(NSDictionary *)job
{
    sleep(1);           // This won't block the main thread. Yay!
    
    // Wrap your job processing in a try-catch. Always use protection!
    @try {
        if ([[job objectForKey:@"task"] isEqualToString:@"success"]) {
            return EDQueueResultSuccess;
        } else if ([[job objectForKey:@"task"] isEqualToString:@"fail"]) {
            return EDQueueResultFail;
        }
    }
    @catch (NSException *exception) {
        return EDQueueResultCritical;
    }
    
    return EDQueueResultCritical;
}

SomewhereElse.m

[[EDQueue sharedInstance] enqueueWithData:@{ @"foo" : @"bar" } forTask:@"nyancat"];

In order to keep things simple, the delegate method expects a return type of EDQueueResult which permits three distinct states:

  • EDQueueResultSuccess: Used to indicate that a job has completed successfully
  • EDQueueResultFail: Used to indicate that a job has failed and should be retried (up to the specified retryLimit)
  • EDQueueResultCritical: Used to indicate that a job has failed critically and should not be attempted again

Handling Async Jobs

As of v0.6.0 queue includes a delegate method suited for handling asyncronous jobs such as HTTP requests or Disk I/O:

- (void)queue:(EDQueue *)queue processJob:(NSDictionary *)job completion:(void (^)(EDQueueResult))block
{
    sleep(1);
    
    @try {
        if ([[job objectForKey:@"task"] isEqualToString:@"success"]) {
            block(EDQueueResultSuccess);
        } else if ([[job objectForKey:@"task"] isEqualToString:@"fail"]) {
            block(EDQueueResultFail);
        } else {
            block(EDQueueResultCritical);
        }
    }
    @catch (NSException *exception) {
        block(EDQueueResultCritical);
    }
}

Introspection

As of v0.7.0 queue includes a collection of methods to aid in queue introspection specific to each task:

- (Boolean)jobExistsForTask:(NSString *)task;
- (Boolean)jobIsActiveForTask:(NSString *)task;
- (NSDictionary *)nextJobForTask:(NSString *)task;

Methods

- (void)enqueueWithData:(id)data forTask:(NSString *)task;

- (void)start;
- (void)stop;
- (void)empty;

- (Boolean)jobExistsForTask:(NSString *)task;
- (Boolean)jobIsActiveForTask:(NSString *)task;
- (NSDictionary *)nextJobForTask:(NSString *)task;

Delegate Methods

- (EDQueueResult)queue:(EDQueue *)queue processJob:(NSDictionary *)job;
- (void)queue:(EDQueue *)queue processJob:(NSDictionary *)job completion:(void (^)(EDQueueResult result))block;

Result Types

EDQueueResultSuccess
EDQueueResultFail
EDQueueResultCritical

Properties

@property (weak) id<EDQueueDelegate> delegate;
@property (readonly) Boolean isRunning;
@property (readonly) Boolean isActive;
@property NSUInteger retryLimit;

Notifications

EDQueueDidStart
EDQueueDidStop
EDQueueDidDrain
EDQueueJobDidSucceed
EDQueueJobDidFail

iOS Support

EDQueue is designed for iOS 5 and up.

ARC

EDQueue is built using ARC. If you are including EDQueue in a project that does not use Automatic Reference Counting (ARC), you will need to set the -fobjc-arc compiler flag on all of the EDQueue source files. To do this in Xcode, go to your active target and select the "Build Phases" tab. Now select all EDQueue source files, press Enter, insert -fobjc-arc and then "Done" to enable ARC for EDQueue.

More Repositories

1

sentiment

AFINN-based sentiment analysis for Node.js.
JavaScript
2,641
star
2

color

A collection of categories and utilities that extend UIColor
Objective-C
537
star
3

troll

Language sentiment analysis and neural networks... for trolls.
JavaScript
332
star
4

cam

A β€œkeep it simple” approach to handling photo and video capture with AVFoundation.
Objective-C
280
star
5

storage

An iOS library for fast, easy, and safe threaded disk I/O.
Objective-C
257
star
6

semver

Semantic Versioning library for Objective-C
Objective-C
115
star
7

washyourmouthoutwithsoap

A list of bad words in many languages.
JavaScript
96
star
8

fastly

Fastly API client for Node.js
JavaScript
70
star
9

conduit

JS to Objective-C... and back again.
Objective-C
52
star
10

parallax

Objective-C library for implementation of CoreMotion-controlled parallax distortion.
Objective-C
44
star
11

generator

Language agnostic project bootstrapping with an emphasis on simplicity.
JavaScript
30
star
12

fork-pool

A generic child process pool for Node.js.
JavaScript
27
star
13

logo

A streaming parser for the LOGO programming language.
JavaScript
23
star
14

micron-throttle

Token bucket based HTTP request throttle for Node.js
JavaScript
16
star
15

trebuchet

A node.js module for throwing email around using the Postmark API.
JavaScript
15
star
16

rodeo

Realtime notifications with Redis and Node.js
JavaScript
14
star
17

turtle

A collaborative programming environment for the LOGO programming language.
JavaScript
14
star
18

orchestra

Keyboard-based instruments designed for MaKey MaKey
10
star
19

basic

HTTP Basic Authentication for Node.js
JavaScript
10
star
20

dpla

Node.js API client for the Digital Public Library of America
JavaScript
9
star
21

graffle-json

A node.js utility for converting OmniGraffle .OO3 files into structured JSON
JavaScript
9
star
22

simple

A simple static HTTP server
JavaScript
8
star
23

tineye

Node.js client for the Tineye search API
JavaScript
8
star
24

strainer

Simple filtering of arrays and object streams.
JavaScript
8
star
25

namebot

A node.js module for creating usernames based on a specified corpus
JavaScript
7
star
26

baseit

A node.js module for simple(r) handling of radix 2 through 36 base encodings.
JavaScript
5
star
27

phidget

Node.js bindings for the Phidget line of USB sensor and control interfaces.
JavaScript
5
star
28

friendly-phonemes

A kid friendly corpus in both JSON and phonetic "DICT" formats
5
star
29

assert

Assertion extensions and utilities for OCUnit
Objective-C
4
star
30

cc-client

Node.js client for the Constant Contact API
JavaScript
3
star
31

3d-mixer

OpenFrameworks based 8-channel 3D sound mixer prototype
C
3
star
32

php-console

PHP Console is a MacOS X (10.6+) Cocoa application that provides users with a simple environment in which to execute arbitrary PHP code.
Objective-C
3
star
33

up-client

Node.js client for the (unofficial) Jawbone UP API
JavaScript
3
star
34

rij

Safe and sensible work queue for Node.js
JavaScript
3
star
35

localq

A persistent job queue for the browser.
JavaScript
3
star
36

cork

An API utility belt for request.
JavaScript
2
star
37

dotfiles

My dotfiles. There are many like them, but these are mine.
Shell
2
star
38

apostle

Node.js API client for Apostle.io
JavaScript
2
star
39

sublime

A collection of handy Sublime Text snippets & build scripts
Python
2
star
40

hipchat-cli

A Hipchat CLI using curl
Shell
2
star
41

vouch

JSON schema validation ... for humans.
JavaScript
2
star
42

micron

Minimalist extensions to the Node.js core HTTP server.
JavaScript
2
star
43

badgecrawler

Search provider for Mozilla Open Badges
JavaScript
2
star
44

randy

Socket.io based realtime notifications with Rodeo.
JavaScript
2
star
45

teach-presentation

How to Teach (Almost) Anything - Presentation Slides
1
star
46

dashboard

gMail to servo = wat
JavaScript
1
star
47

ios-blinkrc-control

Quick prototype iOS control application for the "Insurance Liability Bot" (BlinkRC servo controller). Requires Sparrow framework (http://www.sparrow-framework.org).
Objective-C
1
star
48

uiimage-io

A category for UIImage that provides naive methods for saving UIImage objects to disk. For demo purposes only.
Objective-C
1
star
49

dscripts

A collection of dtrace scripts
D
1
star