Skip to content

Instantly share code, notes, and snippets.

@echoz
Created May 31, 2012 09:35
Show Gist options
  • Save echoz/2842256 to your computer and use it in GitHub Desktop.
Save echoz/2842256 to your computer and use it in GitHub Desktop.
simple thread safe token generator
// _requests is a dictionary that can be mutated on any thread
// __tokenLBCURLRequestMap is just a dictionary to check if the token already is being "processed"
-(NSString *)readyToken {
if ([_requests count] == 0)
return nil;
NSMutableArray *tokens = [[[_requests allKeys] mutableCopy] autorelease];
NSString *token = nil;
while (YES) {
if ([tokens count] == 0)
return nil;
token = [tokens objectAtIndex:0];
[tokens removeObjectAtIndex:0];
if ((![__tokenLBCURLRequestMap objectForKey:token]) && ([_requests objectForKey:token]))
return token;
}
return nil;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment