Anonymous NSConnection using NSApplication's receive NSPort?
Anonymous NSConnection using NSApplication's receive NSPort?
- Subject: Anonymous NSConnection using NSApplication's receive NSPort?
- From: James Bucanek <email@hidden>
- Date: Mon, 2 May 2005 11:16:19 -0700
I have finally caved in and decided to use Distributed Objects and NSConnections for most all of my inter-task and inter-process communications[1]. Gloat if you want. ;)
Before I spend the day implementing new classes around this idea, I wanted to make sure I've got something straight. In looking at setting up NSConnections for connecting objects between threads, I see two kinds of example code:
(1) Anonymous NSConnections using arbitrary NSPorts used to connect two independent NSConnections.
(2) Named NSConnections used to vend objects on the main NSApplication thread.
Some objects I need to vend from the main NSAppliaction thread, because they are objects that might receive state changes from other threads. These changes can occur at any time, but the vended status/receiver object needs to run on the main NSApplication event loop because it might have to do GUI things.
So......
What it seems that I need is to create an NSConnection object the uses two NSPorts: One NSPort that I create out of thin air for the NSConnections to send on, and the receive port of the main NSRunLoop for the NSConnection to get messages on.
I have found code examples that do this, but those examples always create named NSConnections. I don't want named connections, for a variety of reasons. The examples that create anonymous connections always create two independent NSPorts. But I don't want that, because that could mean that state change messages from the other threads would execute asynchronously (i.e. NOT on the NSApplication's run loop thread).
My proposal is to create my NSConnection thusly (from the main NSApplication thread, of course):
NSPort* sendPort = [NSPort port];
NSPort* appKitPort = [[NSConnection defaultConnection] receivePort];
NSConnection* feedbackConnection = nil;
feedbackConnection = [NSConnection connectionWithReceivePort:appKitPort sendPort:sendPort];
[feedbackConnnection setRootObject:stateChangeReceiver];
...
Seems too simple. Am I missing something?
James
[1] I was initially resisting because we have to someday make this server communicate with non-Objective-C clients, which obviously won't be using NSConnection objects. But after some debate I concluded that when that day does come, it shouldn't be too hard to add an abstraction layer and manually encode the data that has to go to the non-Obj-C clients.
--
James Bucanek <mailto:email@hidden>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden