Methods of NWProtocolFramerImplementation vs NWProtocolFramer.Instance

For a given instantiation, is the Instance object passed to each method of NWProtocolFramerImplementaion the same? If that includes the initializer, why do the other methods still want it?

Answered by DTS Engineer in 905131022
is the Instance object passed to each method of NWProtocolFramerImplementaion the same?

Yes.

If that includes the initializer, why do the other methods still want it?

I suspect it’s simply fallout from the way that the Swift API is layered on top of the C API. There’s no C equivalent of NWProtocolFramerImplementation. Rather, in C you create the framer by passing in a closure to nw_framer_create_definition, which in turns is expected to set up the other closures.

Having said that, I think it makes sense to use the instance that’s passed in to each method rather than caching that in a property. That’s how all the framers I’ve ever seen work, and there’s at least one potential pitfall for not following that pattern, namely, accidentally creating a retain loop [1].

Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] I’m not saying that that’s guaranteed to happen, just that all framers I’ve seen use the standard pattern and straying from that path creates a potential risk.

is the Instance object passed to each method of NWProtocolFramerImplementaion the same?

Yes.

If that includes the initializer, why do the other methods still want it?

I suspect it’s simply fallout from the way that the Swift API is layered on top of the C API. There’s no C equivalent of NWProtocolFramerImplementation. Rather, in C you create the framer by passing in a closure to nw_framer_create_definition, which in turns is expected to set up the other closures.

Having said that, I think it makes sense to use the instance that’s passed in to each method rather than caching that in a property. That’s how all the framers I’ve ever seen work, and there’s at least one potential pitfall for not following that pattern, namely, accidentally creating a retain loop [1].

Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] I’m not saying that that’s guaranteed to happen, just that all framers I’ve seen use the standard pattern and straying from that path creates a potential risk.

Methods of NWProtocolFramerImplementation vs NWProtocolFramer.Instance
 
 
Q