AsyncDisplayKit icon indicating copy to clipboard operation
AsyncDisplayKit copied to clipboard

[Question] why is ASDelegateProxy a proxy

Open Yue-Wang-Google opened this issue 10 years ago • 10 comments

Hi, we'd like to know why ASDelegateProxy is a proxy, since it doesn't use any of the NSProxy's methods.

Yue-Wang-Google avatar Aug 16 '16 22:08 Yue-Wang-Google

@garrettmoon @Adlai-Holler @maicki

hannahmbanana avatar Aug 17 '16 06:08 hannahmbanana

@Yue-Wang-Google not sure I understand the question. Are you asking why it's not a subclass of NSProxy? I wrote it to do exactly what I needed (break a retain cycle created by CADisplayLink) and nothing more. Subclassing NSProxy seemed more complex. Is there an issue you're seeing?

garrettmoon avatar Aug 17 '16 17:08 garrettmoon

Oh we were asking why ASDelegateProxy is a subclass of NSProxy rather than a NSObject. Since ASDelegateProxy and its subclasses do not use any NSProxy features. See https://github.com/facebook/AsyncDisplayKit/blob/master/AsyncDisplayKit/Details/ASDelegateProxy.h#L28

Yue-Wang-Google avatar Aug 17 '16 17:08 Yue-Wang-Google

Whoops, I totally misunderstood and thought you were referring to ASWeakProxy.

Looking into it a bit, ASWeakProxy probably should inherit from NSProxy.

NSProxy doesn't inherit from NSObject and implements far fewer methods allowing your proxy object to override more points.

garrettmoon avatar Aug 17 '16 17:08 garrettmoon

@garrettmoon as you pointed out, NSProxy implements a lot less methods than NSObject.

I had an issue in one of my app with ASDelegateProxy. I had some code that was testing the class of a UIScrollView delegate with isKindOf: and that will crash on an NSProxy object (because NSProxy doesn't implement isKindOf). I ended up using isProxy to avoid the crash.

Back to the original question, if ASDelegateProxy doesn't actually use the NSProxy APIs, do we have a substantial added benefit of having it an NSProxy instead of an NSObject ?

Note: I'm not very familiar with NSProxy, so there is potentially one (or several) very valid reason to use it.

jmig avatar Aug 17 '16 19:08 jmig

What do you mean, it doesn't use the NSProxy APIs? NSProxy doesn't have specific APIs that a subclass would call, it's intention appears to be a minimal object to support proxying – more minimal than even NSObject (it doesn't even have an init method!). So the advantage is it is lightweight and allows for proxying methods on NSObject without explicitly overriding them. At least that's my understanding :)

garrettmoon avatar Aug 17 '16 19:08 garrettmoon

Like I said I'm not an expert in NSProxy so I'm gonna lean toward your advice.

I was under the impression that the interesting APIs for NSProxy were methodSignatureForSelector: and forwardInvocation:. In the case of ASDelegateProxy, forwardInvocation is basically a no-op.

Thanks for the explanation, I definitely read more about NSProxy today than I ever did in the past 8 years ^^.

jmig avatar Aug 17 '16 19:08 jmig

Ah, found a stack overflow that might explain better what I was trying to say:

http://stackoverflow.com/questions/18284724/nsproxy-vs-nsobject

Essentially, if we wanted to proxy anything that NSObject implemented, we'd need to override that method and call it on our proxy object. By not subclassing NSObject we can avoid that because forwardingTargetForSelector: will get called. If it was implemented in NSObject and we didn't override it, forwardingTargetForSelector: would never be called.

garrettmoon avatar Aug 17 '16 19:08 garrettmoon

Thanks @garrettmoon

jmig avatar Aug 17 '16 20:08 jmig

Thanks @garrettmoon for your explanation!

Yue-Wang-Google avatar Aug 17 '16 21:08 Yue-Wang-Google