Streamlyne / Cocoa-SDK

Streamlyne API for Cocoa Developers
https://github.com/Streamlyne/Cocoa-SDK
2 stars 1 forks source link

Use PromiseKit #9

Closed Glavin001 closed 10 years ago

Glavin001 commented 10 years ago

https://github.com/mxcl/PromiseKit

Glavin001 commented 10 years ago

From http://promisekit.org/

@interface AFHTTPRequestOperation (PromiseKit)

- (PMKPromise *)promise {
    return [Promise new:^(PromiseFulfiller fulfiller, PromiseRejecter rejecter){
        [self setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *op, id responseObject){

            // PMKManifold allows you to fulfill promises with
            // multiple parameters.
            fulfiller(PMKManifold(responseObject, op));

            // We inverted the parameter order relative to the
            // AFNetworking completionBlock because promises order
            // their parameters from most to least important. Thus
            // consumers can `then` without the second parameter
            // if they don’t need it.

        }) failure:^(AFHTTPRequestOperation *op , NSError *error){
            rejecter(error);
        }];
    }];
}

@end
Glavin001 commented 10 years ago

Use http://blog.dadabeatnik.com/2013/09/12/xcode-and-asynchronous-unit-testing/ for async unit testing.

// Set the flag for a block completion handler
#define StartBlock() __block BOOL waitingForBlock = YES

// Set the flag to stop the loop
#define EndBlock() waitingForBlock = NO

// Wait and loop until flag is set
#define WaitUntilBlockCompletes() WaitWhile(waitingForBlock)

// Macro - Wait for condition to be NO/false in blocks and asynchronous calls
#define WaitWhile(condition) 
do { 
    while(condition) { 
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; 
    } 
} while(0)

Updated: https://gist.github.com/Phillipus/6537635

Glavin001 commented 10 years ago

Get excited for Xcode 6! Built-in async unit test: http://blog.dadabeatnik.com/2014/07/13/asynchronous-unit-testing-in-xcode-6/