A-IOSHelper [Animax Helper]

This is a scaffold library and the aid of the project is providing all foundation functions for IOS developer to build app easier.
Stories in Ready Build Status


Project maintained by Animaxx Thanks mattgraham for original Theme

Tasks

Tasks helper provides simple implement of tasks chain with async and sync.



Contents

Rapid request network asynchronously example

    [A_TaskHelper A_RunInBackground:^id{
        NSDictionary* _dic = [A_RESTRequest A_GetDictionary:@"www.yourwebservice.com/something"
            Parameters:@{@"key1":@"value", @"key2": @"value2"}
            Headers:@{@"Accept":@"application/json",
                @"Content-Type":@"application/json; charset=utf-8"}];
        return _dic;
    } WhenDone:^(id arg) {
        NSDictionary* _dic = (NSDictionary*)arg;
        // Displaying in UI Thread
    }];


Rapid asynchronously operation example

    [A_TaskHelper A_RunInBackgroundWithParam:@"Arguments"
        Block:^id(id arg) {
            return [arg stringByAppendingString:@" with result"];
        } WhenDone:^(id arg, id result) {
            NSLog(@"%@", result); // Arguments with result
        }];


Delay execute example

// Delay 1.3 seconds and pop-up an alert box
[A_TaskHelper A_DelayExecuteWithObj:@"Message here!" Method:^(id arg) {
    [A_AlertBox A_SystemAlert:(NSString*)arg AndTitle:@"Alert"];
} Delay:1.3f];


Tasks chain mutil-steps example

A_TaskHelper* task = [A_TaskHelper A_Init:A_Task_RunInBackgroup Sequential:YES];
    [task A_AddDelayTask:2.0f Block:^(A_TaskHelper *task) {
            NSLog(@"Task %d",1);
    }];
    [task A_AddDelayTask:1.0f Block:^(A_TaskHelper *task) {
            NSLog(@"Task %d",2);
    }];
    [task A_AddTask:^(A_TaskHelper *task) {
            NSLog(@"Task %d",3);
    }];

    CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();

    [task A_ExecuteWithCompletion:^(A_TaskHelper *task) {
        CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
        NSLog(@"operation took %2.5f seconds", end-start);
    }];
    /*
        The result shows tasks are running in order, because the queue is in Sequential Mode.

        Output:
            xctest[4323:138683] Task 1
            xctest[4323:138683] Task 2
            xctest[4323:138683] Task 3
            xctest[4323:138683] operation took 3.19355 seconds
    */

    /*
        If set the Sequential to NO, then the result will be difference.

        Output:
            xctest[4499:153364] Task 3
            xctest[4499:153363] Task 2
            xctest[4499:153370] Task 1
            xctest[4499:153363] operation took 2.09852 seconds
    */
        

Tasks chain example

    // By default, task is set as A_Task_RunInBackgroupCompleteInMain and Sequential
    [[[[A_TaskHelper A_Init] A_AddTask:^(A_TaskHelper *task) {
        task.Tag = @(1);
    }] A_AddTask:^(A_TaskHelper *task) {
        task.Tag = @([((NSNumber*)task.Tag) integerValue] + 1);
    }] A_ExecuteWithCompletion:^(A_TaskHelper *task) {
        NSLog(@"Tag: %@", task.Tag);
    }];