Question

I am attempting to vend an object across two processes, within my application. However, when I test my code the process that receives the vended object just blocks. I've more or less followed the example code on http://www.mikeash.com/pyblog/friday-qa-2009-02-20-the-good-and-bad-of-distributed-objects.html.

Below is the code for my two processes:

/*
 * Description: Vends an object that the receiver can then access
 *              through the distributed object.
 */

#import <Cocoa/Cocoa.h>
#import <iostream>

using namespace std;

int main() {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    cout << "Starting vendor " << endl;

    NSMutableArray *mutable_array;
    [mutable_array addObject:@"Louis Lang"];
    [mutable_array addObject:@"John Doe"];

    NSConnection *connection = [NSConnection connectionWithReceivePort:[NSPort port] sendPort:nil];
    [connection setRootObject:mutable_array];
    [connection registerName:@"com.example.whatever"];

    [[NSRunLoop currentRunLoop] run];


    [pool drain];

    return 0;
}

And the "receiver"

/*
 * Description: Receives the vended object from the server
 *
 */

#import <Cocoa/Cocoa.h>
#import <iostream>

using namespace std;

int main() {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    cout << "Starting receiver." << endl;

    id theObject = (id)[NSConnection rootProxyForConnectionWithRegisteredName:@"com.example.whatever" host:nil];

    int the_count = [theObject count];

    NSLog(@"There are %i items in mutable_array", the_count);

    [pool drain];

    return 0;
}
Was it helpful?

Solution

Your code seems OK for most of it, the distributed objects part at least. But:

NSMutableArray* mutable_array = [[NSMutableArray alloc] init];
[mutable_array addObject:@"Louis Lang"];
[mutable_array addObject:@"John Doe"];

will definitely help, I think.

If you are using Xcode 4.4+, this will do as well:

NSArray* array = @[ @"Louis Lang", @"John Doe" ];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top