Latest 1.0.1
Homepage https://github.com/DarkKnightOne/NPDispatcher
License MIT
Platforms ios 8.0
Authors

CI Status
Version
License
Platform

NPDispatcher is a task dispatcher library for iOS and Mac OS X, It can limit max count of running tasks at the same time.

Choose NPDispatcher for your next project, you’ll be happy you did!

Example Project

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

NPDispatcher Version Minimum iOS Target Minimum OS X Target Minimum watchOS Target Minimum tvOS Target Notes
1.x iOS 8 OS X 10.9 watchOS 2.0 tvOS 9.0 Xcode 7+ is required.

Installation with CocoaPods

CocoaPods is a dependency manager for Objective-C, which automates and simplifies the process of using 3rd-party libraries like NPDispatcher in your projects.You can install it with the following command:

$ gem install cocoapods

CocoaPods 0.39.0+ is required to build NPDispatcher 1.0.1.

Podfile

To integrate NPDispatcher into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'

target 'TargetName' do
pod 'NPDispatcher', '~> 1.0.1'
end

Then, run the following command:

$ pod install

Usage

NPTask

NPTask is a carrier for task, distinguish the task with Identifier. create the task with following method and start it.

+ (id)taskWithIdentifier:(NSUInteger)identifier dataObject:(id)data;
- (id)initWithIdentifier:(NSUInteger)identifier dataObject:(id)data;
- (void)start;

And when the task complete, you can commit(success) or fail it.

- (void)commit;
- (void)fail:(NSError *)error;

NPDispatcher

NPDispatcher is the manager of task running and pending.      

      

1 . initial it. set max count of the running tasks at the same time and new task dispatched call back.

+ (NPDispatcher *)dispatcherWithMaxTaskCount:(NSUInteger)maxCount taskArrivedCallBack:(NPTaskArrivedCallBack)callBack;
- (NPDispatcher *)initWithMaxTaskCount:(NSUInteger)maxCount taskArrivedCallBack:(NPTaskArrivedCallBack)callBack;

2 . add tasks.

- (void)addTask:(NPTask *)task;
- (void)addTasks:(NSArray<NPTask *> *)tasks;
- (void)insertTask:(NPTask *)task atIndex:(NSInteger)index; 

3 . remove task when complete.

- (void)removeTask:(NPTask *)task;

4 . cancel not start tasks if not necessary to execute.

- (NSArray<NPTask *> *)cancelNotStartTasks;

Usage Example

1 . Define

@property (nonatomic, strong) NPDispatcher *dispatcher;

2 . Initial

__weak typeof(self)weakSelf = self;
_dispatcher = [NPDispatcher dispatcherWithMaxTaskCount:5 taskArrivedCallBack:^(NSArray<NPTask *> *tasks) {
NSUInteger taskCount = tasks.count;
for (NSUInteger index = 0; index < taskCount; index ++) {
NPTask *task = [tasks firstObject];
//If Success
[task commit];
//If Fail
[task fail:[NSError errorWithDomain:@"NPDispatcher" code:-1 userInfo:@{@"error" : @"I`m uncareful failed."}]];
}
if (tasks.count == 0) {
NSLog("All of task is complete.");
}
}];

3 . Add Tasks

NSMutableArray<NPTask *> *testTasks = [NSMutableArray arrayWithCapacity:10];
for (int index = 0; index < 10; index ++) {
//1. Create task
NPTask *task = [NPTask taskWithIdentifier:index dataObject:nil];
//2. Start task
[task start];
//3. Set success call back
task.successCallBack = ^(NPTask *t) {
NSLog(@"task %lu success.", t.identifier);
//6. Remove complete task from dispatcher
[weakSelf.dispatcher removeTask:t];
};
//4. Set failed call back
task.failedCallBack = ^(NPTask *t, NSError *error) {
NSLog(@"task %lu failed:%@.", t.identifier, t.error.description);
//6. Remove complete task from dispatcher
[weakSelf.dispatcher removeTask:t];
};
[testTasks addObject:task];
}
//5. Add tasks
[_dispatcher addTasks:testTasks];

Author

zhang.wenhai, [email protected]

License

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

Latest podspec

{
    "name": "NPDispatcher",
    "version": "1.0.1",
    "summary": "A task dispatcher for iOS, OS X",
    "description": "A task dispatcher for iOS, OS. nCan control running task max count",
    "homepage": "https://github.com/DarkKnightOne/NPDispatcher",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "zhang.wenhai": "[email protected]"
    },
    "source": {
        "git": "https://github.com/DarkKnightOne/NPDispatcher.git",
        "tag": "1.0.1"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "NPDispatcher/Classes/**/*"
}

Pin It on Pinterest

Share This