Adopting NSSecureCoding with opaque types

330 views Asked by At

I have an Objective-C class that needs to adopt NSSecureCoding for transport across an XPC connection. The class has a couple properties that are opaque types (dispatch_queue_t and dispatch_group_t).

How would I go about implementing -initWithCoder: and -encodeWithCoder: in this case? The documentation says that these are object-like structures so I'm assuming they would need to be converted to/from raw bytes when encoding/decoding? If this is the case, I'm not sure which methods should be used to do this. Another option would be to simply avoid encoding them at all and recreate them inside -initWithCoder:.

1

There are 1 answers

0
ipmcc On BEST ANSWER

There's not a general solution for all opaque types, but you mentioned dispatch_queue_t and dispatch_group_t. If the dispatch queue and group are private to the coded object, then just recreate them in -initWithCoder: like Kurt Revis said. If they're shared, it is a little bit more hairy, but you can sometimes work around it by archiving a "surrogate" object, then traversing your object graph after decoding it, replacing all references to the surrogate with references to a single, newly-created "real" object.

Really though, this sounds suspiciously like an anti-pattern. Classes that implement NSCoding/NSSecureCoding would typically be model classes, and it's kind of a red flag that there are dispatch queues and groups in ivars of a model class. I can think of some legitimate reasons to have a private queue (i.e. serializing multithreaded access to internal state) in a model class ivar, but not a shared one. In general, it sounds like some re-factoring might be in order.