Home > DeveloperSection > Forums > Call Back funtion handling
monica frank
monica frank

Total Post:10

Posted on    October-14-2015 12:06 AM

 iPhone iPhone  Ios  Objective C 

 1 Reply(s)
 708  View(s)
Rate this:

I have created 2 callback functions in Obj-C, first callback function works fine but 2nd callback function generating problem.
Please help whats going wrong?
For example:

cb = IOServiceAddMatchingNotification(notifyPort, firstMatchNotification,
                                     matchingDict, RawDeviceAdded, NULL,
RawDeviceAdded(NULL, gRawAddedIter, self);

This works fine. But below function receives self as nil.

cb = IOServiceAddMatchingNotification(notifyPort, firstMatchNotification,

BulkTestDeviceAdded(NULL, gBulkTestAddedIter, self);

Tarun Kumar

Total Post:214

Posted on    October-14-2015 11:28 PM

I am guessing that you are using a common method to handle callback functions, it’s a static function that forwards your instance using callback.
Ok, here I try to solve your problem, I think it’s useful for you, here is the code:

static RawDeviceAdded(void* refcon, io_iterator_t iterator) {
    [(MyClass*)refcon rawDeviceAdded:iterator];
@implementation MyClass
- (void)setupCallbacks {
    // ... all preceding setup snipped
    cb = IOServiceAddMatchingNotification(notifyPort,firstMatchNotification, matchingDict, RawDeviceAdded, (void*)self, &gRawAddedIter );
    // call the callback method once to 'arm' the iterator
    [self rawDeviceAdded:gRawAddedIterator];
- (void)rawDeviceAdded:(io_iterator_t)iterator {
    // take care of the iterator here, making sure to complete iteration to re-arm it

Don't want to miss updates? Please click the below button!

Follow MindStick