Parag Bafna
Parag Bafna

Reputation: 22930

How to get data from c function to cocoa(objective c) function

I am using CFMessagePortRef for inter process communication. for receiving data , i have call back method

CFDataRef didReceiveDataFromOtherProcess(CFMessagePortRef local,SInt32 msgid,CFDataRef data,void *info)

Now i want to send received data to my main controller. i can create main controller object in didReceiveDataFromOtherProcess and send data to main controller, but i want to write generalize message passing module. i am not able to access instance variable in callback function, delegate will not work in this case, so i defined -(void)saveObject:(id)object method and saving object of main controller in global variable.

messagePassing *object = [[messagePassing alloc]init];
[object saveObject:self]; //in main controller

//in messagePassing
-(void)saveObject:(id)object
{
globalObject = object;
}

CFDataRef didReceiveDataFromOtherProcess(CFMessagePortRef local,SInt32 msgid,CFDataRef data,void *info)
{
//....
[globalObject didReceivedData:(id)data]; // sending to main controller
}

but in this case; if i will open two connection it will change my global object.
Can anyone please help me out?

Upvotes: 1

Views: 170

Answers (1)

Rob Napier
Rob Napier

Reputation: 299325

This is what info is for. Pass self as the info pointer when you set up the callback. Then dereference it in the callback so that you can communicate with the originating object.

Be careful about memory management. If self is deallocated before the callback, you'll crash when you deallocate it. Make sure to remove your callback registration in dealloc so this can't happen.

Upvotes: 1

Related Questions