Closed Glavin001 closed 10 years ago
@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
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)
Get excited for Xcode 6! Built-in async unit test: http://blog.dadabeatnik.com/2014/07/13/asynchronous-unit-testing-in-xcode-6/
https://github.com/mxcl/PromiseKit