Created
October 29, 2012 15:05
-
-
Save kwylez/3974049 to your computer and use it in GitHub Desktop.
BROKEN example of concurrent code. This is for comparison ONLY
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ModelManager.m | |
// ThreadingHell | |
// | |
// Created by Cory D. Wiles on 10/19/12. | |
// Copyright (c) 2012 Cory D. Wiles. All rights reserved. | |
// | |
#import "ModelManager.h" | |
#import "NSMutableString+CoryAdditions.h" | |
static NSString* const wsRequestErrorDomain = @"net.epb.imf.im3.request.error"; | |
static char const *imfQueueName = "net.epb.imf.im3.queue"; | |
static dispatch_queue_t _imfDispatchQueue; | |
@implementation ModelManager | |
+ (void)initialize { | |
_imfDispatchQueue = dispatch_queue_create(imfQueueName, DISPATCH_QUEUE_CONCURRENT); | |
} | |
+ (ModelManager *)sharedManager { | |
static ModelManager *_sharedClient = nil; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
_sharedClient = [[self alloc] init]; | |
}); | |
return _sharedClient; | |
} | |
- (void)getAccountDetailsForAccountNumber:(NSString* )accountNumber | |
completionBlock:(IMFRequestCompletionBlock)completion { | |
dispatch_sync(_imfDispatchQueue, ^{ | |
NSLog(@"trying to fetch information for details"); | |
NSMutableString *details = [NSMutableString randomStringWithCapacity:10]; | |
[details appendFormat:@" details"]; | |
NSLog(@"details string: %@", details); | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
if (completion) { | |
if (details) { | |
completion(details, YES, nil); | |
} else { | |
NSString *errorMessage = [NSString stringWithFormat:@"Failed getting details for account: %@", accountNumber]; | |
NSError *failerror = [NSError errorWithDomain:wsRequestErrorDomain | |
code:1002 | |
userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(errorMessage, nil) | |
forKey:NSLocalizedDescriptionKey]]; | |
completion(nil, NO, failerror); | |
} | |
} | |
}); | |
}); | |
} | |
- (void)getAccountListMetaInformation:(IMFRequestCompletionBlock)completion { | |
__block NSMutableDictionary *accountList = [NSMutableDictionary new]; | |
__block NSMutableArray *accountDetails = [NSMutableArray new]; | |
NSArray *loginResponse = @[@"account1", @"account2"]; | |
if (loginResponse) { | |
NSLog(@"inside loginResponse"); | |
dispatch_sync(_imfDispatchQueue, ^{ | |
[loginResponse enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){ | |
NSLog(@"inside account loop for obj: %@", obj); | |
[self getAccountDetailsForAccountNumber:obj completionBlock:^(id obj, BOOL success, NSError *error) { | |
if (success) { | |
NSLog(@"success account looop"); | |
[accountDetails addObject:obj]; | |
} else { | |
NSLog(@"failed to get any accont details from account: %@ %@", obj, [error localizedDescription]); | |
} | |
}]; | |
}]; | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
NSLog(@"should be updating main queue"); | |
if (completion) { | |
if (accountDetails.count) { | |
completion(accountList, YES, nil); | |
} else { | |
NSString *errorMessage = @"There was error getting your accounts. Please login again."; | |
NSError *failerror = [NSError errorWithDomain:wsRequestErrorDomain | |
code:1003 | |
userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(errorMessage, nil) | |
forKey:NSLocalizedDescriptionKey]]; | |
completion(nil, NO, failerror); | |
} | |
} | |
}); | |
}); | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment