Popular Tags

Who's online

There are currently 0 users and 65 guests online.

User login

1. Introduction
2. Vending an Object
3. Getting a Vended Object
4. A Solution
5. Resources
6. Questions

The idea is that you have a server application that creates an object and then you serve it to the network (or internet if your network is set up in this manner) and then any application in the network can see the object and send methods to the object just like the object is local . (In reality, the client app creates a local object that sends all methods to the server object.) There is no special code that you have to call in order to access a method on the served object. Just call the method like any other method:

  /* Call myMethod. */
  [servedObject myMethod]

Vending an Object

Here is how you can serve an object from the server:

  /* Create a connection. */
  NSConnection *theConnection = [NSConnection defaultConnection];
  /* Specify the object to be served. */
  [theConnection setRootObject: servedObject];
  /* Here we try to register the name of the connection with the network */
  if ([theConnection registerName: @"MyServer"] == NO) {
    NSLog(@"Unable to register as 'MyServer'");
    NSLog(@"Perhaps another copy of this program is running.");

In order to get the object to be shared to other machines you would use this code:
  NSSocketPort *port = [[NSSocketPort alloc] init];
  NSConnection *connection = [NSConnection connectionWithReceivePort:port
  [[NSSocketPortNameServer sharedInstance] registerPort:port
  [connection setRootObject: servedObject];

Getting a Vended Object
That was easy. Now here is how you get the object from the client:

  /* Get the served object */
  id servedObject;
  servedObject = [[NSConnection rootProxyForConnectionWithRegisteredName:@"MyServer"
                                                                    host:nil] retain];

To get an object from another machine:
  id servedObject;
  NSSocketPort *port = (NSSocketPort *)[[NSSocketPortNameServer sharedInstance]
  NSConnection *connection = [NSConnection connectionWithReceivePort:nil
  servedObject = [[connection rootProxy] retain];

That was even easier. Now you can call any method that you need to. It is important to note that this simple of the code will have warnings upon compilation that will say that your method may not work. To fix this you can create a protocol in the client app that has declarations of all of the methods that you need to call.

@protocol ServedObject
  -(void) myMethod;

A Solution

What is the solution to the error above?

Now this is just my theory, but it worked for me. Client apps have their own objects in local memory. Along with this local object is a pointer to the local memory location. If you wanted to have a DO be set to a variable in another DO you can't just set them like you would in a single computer system. What I did is create a method that finds the memory pointer of the object being add from the servers view and have the server do all of the work.



Would it be possible to distribute an NSManagedObjectContext (the primary holding structure for a Core Data model) in this way? If you could have multiple client applications accessing the same Core Data model, you could potentially create some really sick apps.

It is possible, but the application will not work (or at least I can't get it to), and here is my explanation of why.

The way Object Oriented languages such as Cocoa work is that you can store different objects inside of others so that you can easily communicate between them. What you are really storing is actually a memory address location of where the object is stored.

When you distribute an object all you're really doing is creating a trail to the object that is being distributed. Each machine has objects in its local memory that link a proxy object on the client with the real object on the server, which makes the objects in question look like the following:

The problem that occurs is when you want one of the objects inside of the NSSet of the NSManagedObjectContext object. You can ask for one of the objects, but what you get back is a location in memory (see the text underneath the objects). When the client looks up that object in its own memory, it can't find it. So all we need to do is write a "NSDistributedManagedContext" class that takes those objects and actually turns them into Distributed Objects so that the client can use them.

DOCoreData_0.png122.19 KB
messageflow_0.gif9.8 KB
No votes yet



The site has recently been updated. You may notice some of the following issues:

  • Some URLs no longer work. Please use the search box.
  • File uploads should now be working. If you experience problems, please contact us.