Search code examples
iosmacosnsdatacore-foundationcfdata

Get notifications when an NSData/CFData object is being read?


I'm using an API that takes a CFData object, and I need to generate the data on the fly, which is a time consuming operation. The CFData is read by the consumer in a random, non-contiguous manner, and it rarely actually needs the entire length of the data -- it just needs certain random pieces of it.

I would like to improve performance by only actually generating the bits that are requested by the consumer, as they are requested.

Is there any way to subclass CFData/NSData such that I would get callbacks as data chunks are being read and generate them on the fly?

Update: Unfortunately the consumer is 3rd party code and so other classes aren't an option, unless they're somehow magically compatible with CFData.


Solution

  • I don't think you're going to like the answers you get here. You seem to be cornered with respect to options.

    What we know about your situation

    1. The client's code cannot accept a different type / class
    2. CFData is a CoreFoundation C-type and doesn't have a runtime to play with
    3. The client probably uses CoreFoundation symbols to read this data
      1. If you're lucky, it's through the Foundation APIs in Obj-C which has a runtime
    4. CFData wraps a void * to data, but has some padding at the beginning (likely a struct) that'll be difficult to mimic if you tried to create a CFData proxy object

    It's going to be very difficult to accomplish what you want without some clever, un-maintainable, and overall dangerous symbol hackery I won't even bother to get into.