MIDI receive callback context

Up until iOS 14, the signature of MIDI message receive callbacks was the following:

typedef void (*MIDIReadProc)(const MIDIPacketList *pktlist, void *readProcRefCon, void *srcConnRefCon);

where readProcRefCon was an arbitrary pointer that could be used to capture context such as a pointer to a C++ object to call into, or even a function pointer encapsulating that call.

However, this has now been deprecated and replaced with the following:

typedef void (^MIDIReceiveBlock)(const MIDIEventList *evtlist, void *srcConnRefCon);

The readProcRefCon parameter has now been removed, making it far from straightforward to pass messages along to other services without resorting to some sort of global variable, which would be an anti-pattern. So, is there some way to avoid that?

Documentation for the old signature is here and for the new signature is here. No explanation is given as to why the context pointer was removed.

Answered by OOPer in 700074022

No explanation is given as to why the context pointer was removed.

One possible reason may be that the block can capture the context pointer, so you have no need to get it from a parameter of callback function.

Accepted Answer

No explanation is given as to why the context pointer was removed.

One possible reason may be that the block can capture the context pointer, so you have no need to get it from a parameter of callback function.

A number of the CoreMIDI functions have been modified to use blocks. The block is then run under the high priority queue used for running the virtual devices. Notification of changes to the MIDI configuration has also been replaced by a block. See https://bradleyross.github.io/ObjectiveC-Examples/Documentation/BRossTools/FunctionalArea.html

MIDI receive callback context
 
 
Q