Stephen Furlani
Stephen Furlani

Reputation: 6856

Use ASIHTTPRequest as NSDictionary Key

I'm attempting to use a ASIHTTPRequest/ASIFormDataRequest as a key in a NSDictionary as so:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:GPUserData.userlogin forKey:@"username"];
[request setPostValue:GPUserData.password forKey:@"password"];
[request setDelegate:self];
[_delegates setObject:delegate forKey:request];
[_selectors setObject:[NSValue valueWithPointer:selector] forKey:request];

[request startAsynchronous];

But, it doesn't return the right data (nils or random) when I try to access [_delegate objectForKey:request] from within -requestFinished:, so I'm using as a temp solution:

[_delegates setObject:delegate forKey:[request url]];
[_selectors setObject:[NSValue valueWithPointer:selector] forKey:[request url]];

But [request URL] isn't unique per individual request. I know that for something to be a viable key, it needs to have -hash and -isEqual: return the same result, i.e. if the objects return YES to -isEqual: then they must both return the same from -hash.

From ASIFormDataRequest : ASIHTTPRequest : NSOperation : NSObject I can see no method that overrides -isEqual: or -hash from NSObject and ASIHTTPRequest implements (NSCopying) protocol.

So what gives? Why can't I use the request as a key? Is there some other unique identifier for the request that I can use?

Any help is appreciated. For full reference, I'm trying to implement a double-tier callback where the WebServices class parses the data on -requestFinished (and handles network errors and re-logins) and then makes another callback so just the JSON data gets sent as a NSDictionary.

Upvotes: 2

Views: 601

Answers (1)

JosephH
JosephH

Reputation: 37495

Can you instead store a unique value into request.tag, or use the userinfo dictionary to store the data you need to store? eg:

request.userInfo = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"value", @"key", nil];

Upvotes: 4

Related Questions