Add `userInfo` to AFURLConnectionOperation #686

Closed
tewha opened this Issue Dec 17, 2012 · 4 comments

4 participants

@tewha

Detailed background follows, but I'd like a property added to AFURLConnectionOperation, the base NSOperation subclass, for keeping a id<NSObject> userInfo. I suggest copy would be the right semantic.

NSTimer has a similar property.

Detailed background:

I have a need to log requests and responses. I would like to associate them from the called notifications.

AFURLConnectionOperation (and subclasses) seem to be incompatible with NSDictionary. The hash for an object seems stable, but its equality changes. To be compatible with NSDictionary, AFURLConnectionOperation and subclasses would have to implement hash and isEqual: in a stable and unique way. I doubt you'd find agreement on how these should be implemented.

Instead, I am using objc_setAssociatedObject to set a UUID to each AFURLConnectionOperation. Either that, or I need to subclass each subclass of AFURLConnectionOperation I use.

If AFURLConnectionOperation offered a userInfo property, I could avoid these tricks. I think this is a general enough solution that it would be helpful to others.

I'd like to stress here that I've found a solution, I'm looking for a more elegant one. I will not be offended if you don't think this is important or general enough to do. :)

@phoney

I agree with this. I have some metatdata associated with files that are being downloaded. If errors occur this metadata is useful in generating the error message to present to users.

ASIHTTPRequest has a userInfo property. The lack of this property was a stumbling block in my conversion from ASI to AF. My solution was to simply add it to my local copy of AF.

This issue has come up before and Matt is against adding this property. I hope he reconsiders.

@tewha

He told me (via Twitter reply) he''d been considering it for a while. Useful, but feels it's a cop-out.

I agree with him, for what it's worth. If AFNetworking implemented a single class, I think it wouldn't be worth adding. But adding a class cluster means I'd need to subclass every class I use.

Here's something I just thought of that might help you, @phoney: You could probably add your own category to add this property to the base class of the cluster via objc_setAssociatedObject. @mattt, that might make a useful "extra/conttrib" class that would let you not add this to the main object definition.

@blakewatters

Another idea you may want to consider:

Use [NSValue valueWithNonretainedObject:] to obtain a value object for the connection operation. This will give you a stable value that you can use as the key for a dictionary without having to try and tackle the hashing and NSCopying issues that you typically run into with dictionary keys. Its based off of the pointer address, so it will be stable and its easy to implement without any library support,

@mattt

Not sure if this is just me getting caught up in the spirit of giving, but with 732cd2d, I've added a userInfo property.

This is one of those things that especially can't be removed once added to the API, so I'm really hoping I don't live to regret this one. I swear, so help me if I see someone on Stack Overflow suggest caching a request's JSON response object in userInfo.

@blakewatters I'd somehow completely missed NSValue -valueWithNonretainedObject:. Thanks for pointing that one out!

@mattt mattt closed this Dec 26, 2012
@greghe greghe pushed a commit to skillz/AFNetworking that referenced this issue Sep 3, 2015
@mattt mattt [Issue #686][Issue #168] Adding userInfo @property /cc @tewha fe981f5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment