NSConnection Class Reference
| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/Foundation.framework |
| Availability | Available in OS X v10.0 and later. |
| Companion guide | |
| Declared in | NSConnection.h |
Overview
An NSConnection object manages the communication between objects in different threads or between a thread and a process running on a local or remote system. Connection objects form the backbone of the distributed objects mechanism and normally operate in the background. You use the methods of NSConnection explicitly when vending an object to other applications, when accessing such a vended object through a proxy, and when altering default communication parameters. At other times, you simply interact with a vended object or its proxy.
A single connection object may be shared by multiple threads and used to access a vended object.
Tasks
Getting the Default Instance
-
+ defaultConnectionDeprecated in OS X v10.6
Creating Instances
Running the Connection in a New Thread
Vending a Service
-
+ serviceConnectionWithName:rootObject:usingNameServer: -
+ serviceConnectionWithName:rootObject: -
– registerName: -
– registerName:withNameServer: -
– setRootObject: -
– rootObject
Getting a Remote Object
-
+ connectionWithRegisteredName:host: -
+ connectionWithRegisteredName:host:usingNameServer: -
– rootProxy -
+ rootProxyForConnectionWithRegisteredName:host: -
+ rootProxyForConnectionWithRegisteredName:host:usingNameServer: -
– remoteObjects -
– localObjects
Getting a Conversation
Getting All NSConnection Objects
Configuring Instances
-
– setRequestTimeout: -
– requestTimeout -
– setReplyTimeout: -
– replyTimeout -
– setIndependentConversationQueueing: -
– independentConversationQueueing -
– addRequestMode: -
– removeRequestMode: -
– requestModes -
– invalidate -
– isValid
Getting Ports
Getting Statistics
Setting the Delegate
Class Methods
allConnections
Returns all valid NSConnection objects in the process.
Return Value
An array containing all valid NSConnection objects in the process.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSConnection.hconnectionWithReceivePort:sendPort:
Returns an NSConnection object that communicates using given send and receive ports.
Parameters
- receivePort
A receive port.
- sendPort
A send port.
Return Value
An NSConnection object that communicates using receivePort and sendPort.
Discussion
See initWithReceivePort:sendPort: for more information.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSConnection.hconnectionWithRegisteredName:host:
Returns the NSConnection object whose send port links it to the NSConnection object registered with the default NSPortNameServer under a given name on a given host.
Parameters
- name
The name of an
NSConnectionobject.- hostName
The name of the host. The domain name hostName is an Internet domain name (for example, “
sales.anycorp.com”). If hostName isnilor empty, then only the local host is searched for the namedNSConnectionobject.
Return Value
The NSConnection object whose send port links it to the NSConnection object registered with the default NSPortNameServer under name on the host named hostName. Returns nil if no NSConnection object can be found for name and hostName.
The returned NSConnection object is a child of the default NSConnection object for the current thread (that is, it shares the default NSConnection object's receive port).
Discussion
To get the object vended by the NSConnection object, use the rootProxy instance method. The rootProxyForConnectionWithRegisteredName:host: class method immediately returns this object.
Availability
- Available in OS X v10.0 and later.
Declared In
NSConnection.hconnectionWithRegisteredName:host:usingNameServer:
Returns the NSConnection object whose send port links it to the NSConnection object registered under a given name with a given server on a given host.
Parameters
- name
The connection name.
- hostName
The host name.
- server
The name server.
Return Value
The NSConnection object whose send port links it to the NSConnection object registered with server under name on the host named hostName.
Discussion
See connectionWithRegisteredName:host: for more information.
Availability
- Available in OS X v10.0 and later.
Declared In
NSConnection.hcurrentConversation
Returns a token object representing any conversation in progress in the current thread.
Return Value
A token object representing any conversation in progress in the current thread, or nil if there is no conversation in progress.
Availability
- Available in OS X v10.0 and later.
See Also
-
– createConversationForConnection:(NSConnectionDelegate)
Declared In
NSConnection.hrootProxyForConnectionWithRegisteredName:host:
Returns a proxy for the root object of the NSConnection object registered with the default NSPortNameServer under a given name on a given host.
Parameters
- name
The name under which the connection is registered.
- hostName
The host name. The domain name hostName is an Internet domain name (for example,
"sales.anycorp.com"). If hostName isnilor empty, then only the local host is searched for the namedNSConnectionobject.
Return Value
a proxy for the root object of the NSConnection object registered with the default NSPortNameServer under name on the host named hostName, or nil if that NSConnection object has no root object set. Also returns nil if no NSConnection object can be found for name and hostName.
Discussion
The NSConnection object of the returned proxy is a child of the default NSConnection object for the current thread (that is, it shares the default NSConnection object's receive port).
This method invokes connectionWithRegisteredName:host: and sends the resulting NSConnection object a rootProxy message.
Availability
- Available in OS X v10.0 and later.
Declared In
NSConnection.hrootProxyForConnectionWithRegisteredName:host:usingNameServer:
Returns a proxy for the root object of the NSConnection object registered with server under name on a given host.
Parameters
- name
The name of an
NSConnectionobject .- hostName
A host name.
- server
The server.
Return Value
A proxy for the root object of the NSConnection object registered with server under name on the host named hostName, or nil if that NSConnection object has no root object set.
Discussion
See rootProxyForConnectionWithRegisteredName:host: for more information.
Availability
- Available in OS X v10.0 and later.
Declared In
NSConnection.hserviceConnectionWithName:rootObject:
Creates and returns a new connection object representing a vended service on the default system port name server.
Parameters
- name
The name of the service you want to publish.
- root
The object to use as the root object for the published service. This is the object vended by the connection.
Return Value
An NSConnection object representing the vended service or nil if there was a problem setting up the connection object.
Discussion
This method creates the server-side of a connection object and registers it with the default system port name server. Clients wishing to connect to this service can request a communications port from the same port server and use that port to to communicate.
Availability
- Available in OS X v10.5 and later.
See Also
Declared In
NSConnection.hserviceConnectionWithName:rootObject:usingNameServer:
Creates and returns a new connection object representing a vended service on the specified port name server.
Parameters
- name
The name of the service you want to publish.
- root
The object to use as the root object for the published service. This is the object vended by the connection.
- server
The port name server with which to register your service.
Return Value
An NSConnection object representing the vended service or nil if there was a problem setting up the connection object.
Discussion
This method creates the server-side of a connection object and registers it with the specified port name server. Clients wishing to connect to this service can request a communications port from the same port server and use that port to communicate.
If the specified service name corresponds to a service that is autolaunched by launchd, this method allows the service to check in with the launchd process. If the service is not autolaunched by launchd, this method registers the new connection with the specified name. For more information about launchd and its role in launching services, see Daemons and Services Programming Guide
Availability
- Available in OS X v10.5 and later.
Declared In
NSConnection.hInstance Methods
addRequestMode:
Adds mode to the set of run-loop input modes that the receiver uses for connection requests.
Parameters
- mode
The mode to add to the receiver.
Discussion
The default input mode is NSDefaultRunLoopMode. See the NSRunLoop class specification for more information on input modes.
Availability
- Available in OS X v10.0 and later.
See Also
-
addPort:forMode:(NSRunLoop)
Declared In
NSConnection.haddRunLoop:
Adds the specified run loop to the list of run loops the receiver monitors and from which it responds to requests.
Parameters
- runloop
The run loop to add to the receiver.
Discussion
This method is invoked automatically when a request comes in from a new run loop if enableMultipleThreads has been set.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSConnection.hdelegate
Returns the receiver’s delegate.
Return Value
The receiver’s delegate.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSConnection.hdispatchWithComponents:
Allows subclasses to ask a connection object to dispatch component data.
Parameters
- components
Distributed Objects component data.
Discussion
NSPort subclasses should use this method to ask a connection object to dispatch Distributed Objects component data received over the wire. This will decode the data, authenticate, and send the message.
Availability
- Available in OS X v10.7 and later.
Declared In
NSConnection.henableMultipleThreads
Configures the receiver to allow requests from multiple threads to the remote object, without requiring each thread to each maintain its own connection.
Discussion
In OS X v10.5 and later, multiple thread support is enabled by default and this method does nothing.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSConnection.hindependentConversationQueueing
Returns a Boolean value that indicates whether the receiver handles remote messages atomically.
Return Value
YES if the receiver handles remote messages atomically, otherwise NO.
Discussion
See “Configuring a Connection” for more information on independent conversation queueing.
Availability
- Available in OS X v10.0 and later.
Declared In
NSConnection.hinitWithReceivePort:sendPort:
Returns an NSConnection object initialized with given send and receive ports.
Parameters
- receivePort
The receive port for the new connection.
- sendPort
The send port for the new connection.
Return Value
An NSConnection object initialized with receivePort and sendPort. The returned object might be different than the original receiver.
Discussion
The new NSConnection object adds receivePort to the current NSRunLoop object with NSDefaultRunLoopMode as the mode. If the application doesn’t use an NSApplication object to handle events, it needs to run the NSRunLoop object with one of its various run... messages.
This method posts an NSConnectionDidInitializeNotification once the connection is initialized.
The receivePort and sendPort parameters affect initialization as follows:
If an
NSConnectionobject with the same ports already exists, returns it and discards the original receiver.If an
NSConnectionobject exists that uses the same ports, but switched in role, then the newNSConnectionobject communicates with it. Messages sent to a proxy held by either connection are forwarded through the otherNSConnectionobject. This rule applies both within and across address spaces.This behavior is useful for setting up distributed object connections between threads within an application. See Distributed Objects Programming Topics for more information.
If receivePort and sendPort are
nil, deallocates the receiver and returnsnil.If receivePort is
nil, theNSConnectionobject allocates and uses a new port of the same class as sendPort.If sendPort is
nilor if both ports are the same, theNSConnectionobject uses receivePort for both sending and receiving and is useful only for vending an object. Use theregisterName:andsetRootObject:instance methods to vend an object.If an
NSConnectionobject exists that uses receivePort as both of its ports, it’s treated as the parent of the newNSConnectionobject, and its root object and all its configuration settings are applied to the newNSConnectionobject. You should neither register a name for nor set the root object of the newNSConnectionobject. See “Configuring a Connection” for more information.If receivePort and sendPort are different and neither is shared with another
NSConnectionobject, the receiver can be used to vend an object as well as to communicate with otherNSConnectionobjects. However, it has no otherNSConnectionobject to communicate with until one is set up.The receivePort parameter can’t be shared by
NSConnectionobjects in different threads.
This method is the designated initializer for the NSConnection class.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSConnection.hinvalidate
Invalidates the receiver.
Discussion
After withdrawing the ports the receiver has registered with the current run loop, invalidate posts an NSConnectionDidDieNotification and then invalidates all remote objects and exported local proxies.
Availability
- Available in OS X v10.0 and later.
See Also
-
– isValid -
removePort:forMode:(NSRunLoop) -
– requestModes
Declared In
NSConnection.hisValid
Returns a Boolean value that indicates whether the receiver is known to be valid.
Return Value
YES if the receiver is known to be valid, otherwise NO.
Discussion
An NSConnection object becomes invalid when either of its ports becomes invalid, but only notes that it has become invalid when it tries to send or receive a message. When this happens it posts an NSConnectionDidDieNotification to the default notification center.
Availability
- Available in OS X v10.0 and later.
See Also
-
– invalidate -
isValid(NSPort)
Declared In
NSConnection.hlocalObjects
Returns the local objects that have been sent over the connection and still have proxies at the other end.
Return Value
An array containing the local objects that have been sent over the connection and still have proxies at the other end.
Discussion
When an object’s remote proxy is deallocated, a message is sent back to the receiver to notify it that the local object is no longer shared over the connection.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSConnection.hmultipleThreadsEnabled
Returns a Boolean value that indicates whether the receiver supports requests from multiple threads.
Return Value
YES if the receiver supports requests from multiple threads.
Discussion
In OS X v10.5 and later, multiple threads are enabled by default.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSConnection.hreceivePort
Returns the NSPort object on which the receiver receives incoming network messages.
Return Value
The NSPort object on which the receiver receives incoming network messages.
Discussion
You can inspect this object for debugging purposes or use it to create another NSConnection object, but shouldn’t use it to send or receive messages explicitly. Don’t set the delegate of the receive port; it already has a delegate established by the NSConnection object.
Availability
- Available in OS X v10.0 and later.
Declared In
NSConnection.hregisterName:
Registers the specified service using with the default system port name server.
Parameters
- name
The name under which to register the receiver.
Return Value
YES if the operation was successful, otherwise NO (for example, if another NSConnection object on the same host is already registered under name).
Discussion
This method connects the receive port of the receiving NSConnection object with the specified service name. It registers the name using the port name server returned by the systemDefaultPortNameServer method of NSPortNameServer. If the operation is successful, other NSConnection objects can contact the receiver using the connectionWithRegisteredName:host: and rootProxyForConnectionWithRegisteredName:host: class methods.
If the receiver was already registered under a name and this method returns NO, the old name remains in effect. If this method is successful, it also unregisters the old name.
To unregister an NSConnection object, simply invoke registerName: and supply nil as the connection name. Unregistering is currently only supported for NSSocketPort-based connections.
Availability
- Available in OS X v10.0 and later.
See Also
-
– setRootObject: -
– registerName:withNameServer: -
systemDefaultPortNameServer(NSPortNameServer)
Declared In
NSConnection.hregisterName:withNameServer:
Registers a service with the specified port name server.
Parameters
- name
The name under which to register the receiver.
- server
The name server.
Return Value
YES if the operation was successful, otherwise NO (for example, if another NSConnection object on the same host is already registered under name).
Discussion
This method connects the receive port of the receiving NSConnection object with the specified service name. If the operation is successful, other NSConnection objects can contact the receiver using the connectionWithRegisteredName:host: and rootProxyForConnectionWithRegisteredName:host: class methods.
If the receiver was already registered under a name and this method returns NO, the old name remains in effect. If this method is successful, it also unregisters the old name.
To unregister an NSConnection object, simply invoke registerName: and supply nil as the connection name.
Availability
- Available in OS X v10.0 and later.
Declared In
NSConnection.hremoteObjects
Returns all the local proxies for remote objects that have been received over the connection but not deallocated yet.
Return Value
An array containing all the local proxies for remote objects that have been received over the connection but not deallocated yet.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSConnection.hremoveRequestMode:
Removes mode from the set of run-loop input modes the receiver uses for connection requests.
Parameters
- mode
The mode to remove from the set of run-loop input modes the receiver uses for connection requests.
Availability
- Available in OS X v10.0 and later.
See Also
-
– requestModes -
removePort:forMode:(NSRunLoop)
Declared In
NSConnection.hremoveRunLoop:
Removes a given NSRunLoop object from the list of run loops the receiver monitors and from which it responds to requests.
Parameters
- runloop
The run loop to remove from the receiver.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSConnection.hreplyTimeout
Returns the timeout interval for replies to outgoing remote messages.
Return Value
The timeout interval for replies to outgoing remote messages.
Discussion
If a non-oneway remote message is sent and no reply is received by the timeout, an NSPortTimeoutException is raised.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSConnection.hrequestModes
Returns the set of request modes the receiver’s receive port is registered for with its NSRunLoop object.
Return Value
An array of NSString objects that represents the set of request modes the receiver’s receive port is registered for with its NSRunLoop object.
Availability
- Available in OS X v10.0 and later.
See Also
-
– addRequestMode: -
addPort:forMode:(NSRunLoop) -
– removeRequestMode:
Declared In
NSConnection.hrequestTimeout
Returns the timeout interval for outgoing remote messages.
Return Value
The timeout interval for outgoing remote messages.
Discussion
If a remote message can’t be sent before the timeout, an NSPortTimeoutException is raised.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSConnection.hrootObject
Returns the object that the receiver (or its parent) makes available to other applications or threads.
Return Value
The object that the receiver (or its parent) makes available to other applications or threads, or nil if there is no root object.
Discussion
To get a proxy to this object in another application or thread, invoke the rootProxyForConnectionWithRegisteredName:host: class method with the appropriate arguments.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSConnection.hrootProxy
Returns the proxy for the root object of the receiver’s peer in another application or thread.
Return Value
The proxy for the root object of the receiver’s peer in another application or thread.
Discussion
The proxy returned can change between invocations if the peer NSConnection object's root object is changed.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSConnection.hrunInNewThread
Creates and starts a new NSThread object and then runs the receiving connection in the new thread.
Discussion
If the newly created thread is the first to be detached from the current thread, this method posts an NSWillBecomeMultiThreadedNotification with nil to the default notification center.
Availability
- Available in OS X v10.0 and later.
Declared In
NSConnection.hsendPort
Returns the NSPort object that the receiver sends outgoing network messages through.
Return Value
The NSPort object that the receiver sends outgoing network messages through.
Discussion
You can inspect this object for debugging purposes or use it to create another NSConnection object, but shouldn’t use it to send or receive messages explicitly. Don’t set the delegate of the send port; it already has a delegate established by the NSConnection object.
Availability
- Available in OS X v10.0 and later.
Declared In
NSConnection.hsetDelegate:
Sets the receiver’s delegate.
Parameters
- anObject
The receiver’s delegate.
Discussion
A connection’s delegate can process incoming messages itself instead of letting NSConnection object handle them. The delegate can also authenticate messages and accept, deny, or modify new connections.
Availability
- Available in OS X v10.0 and later.
Declared In
NSConnection.hsetIndependentConversationQueueing:
Sets a Boolean value that specifies whether the receiver handles remote messages atomically.
Parameters
- flag
YESif the receiver handles remote messages atomically, otherwiseNO.
Discussion
The default is NO. An NSConnection object normally forwards remote message to the intended recipients as they come in. See “Configuring a Connection” for more information.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSConnection.hsetReplyTimeout:
Sets the timeout interval for replies to outgoing remote messages
Parameters
- seconds
The timeout interval for replies to outgoing remote messages.
Discussion
If a non-oneway remote message is sent and no reply is received by the timeout, an NSPortTimeoutException is raised. The default timeout is the maximum possible value.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSConnection.hsetRequestTimeout:
Sets the timeout interval for outgoing remote messages.
Parameters
- seconds
The timeout interval for outgoing remote messages.
Discussion
If a remote message can’t be sent before the timeout, an NSPortTimeoutException is raised. The default timeout is the maximum possible value.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSConnection.hsetRootObject:
Sets the object that the receiver makes available to other applications or threads.
Parameters
- anObject
The root object for the receiver.
Discussion
This only affects new connection requests and rootProxy messages to established NSConnection objects; applications that have proxies to the old root object can still send messages through it.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSConnection.hstatistics
Returns an NSDictionary object containing various statistics for the receiver.
Return Value
An NSDictionary object containing various statistics for the receiver, such as the number of vended objects, the number of requests and replies, and so on.
Discussion
The statistics dictionary should be used only for debugging purposes.
Availability
- Available in OS X v10.0 and later.
Declared In
NSConnection.hConstants
NSConnection run loop mode
NSConnection defines the following run loop mode—see NSRunLoop for more details.
extern NSString *NSConnectionReplyMode;
Constants
NSConnectionReplyModeThe mode to indicate an
NSConnectionobject waiting for replies.You should rarely need to use this mode.
Available in OS X v10.0 and later.
Declared in
NSConnection.h.
Connection Exception Names
The name of an exception raised in case of authentication failure.
extern NSString *NSFailedAuthenticationException;
Constants
Notifications
NSConnectionDidDieNotification
NSConnection object is deallocated or when it’s notified that its NSPort object has become invalid. The notification object is the NSConnection object. This notification does not contain a userInfo dictionary. An NSConnection object attached to a remote NSSocketPort object cannot detect when the remote port becomes invalid, even if the remote port is on the same machine. Therefore, it cannot post this notification when the connection is lost. Instead, you must detect the timeout error when the next message is sent.
The NSConnection object posting this notification is no longer useful, so all receivers should unregister themselves for any notifications involving the NSConnection object.
Availability
- Available in OS X v10.0 and later.
See Also
-
NSPortDidBecomeInvalidNotification(NSPort)
Declared In
NSConnection.hNSConnectionDidInitializeNotification
NSConnection object is initialized using initWithReceivePort:sendPort: (the designated initializer for NSConnection). The notification object is the NSConnection object. This notification does not contain a userInfo dictionary. Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSConnection.h© 2013 Apple Inc. All Rights Reserved. (Last updated: 2013-01-28)