Created
May 31, 2012 09:35
-
-
Save echoz/2842256 to your computer and use it in GitHub Desktop.
simple thread safe token generator
This file contains hidden or 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
// _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