views:

198

answers:

2

I find myself using a combination of global vars and nsrunloop to force synchronization throughout my application. Although it works it seems a bit ugly to me. Is there any other way of achieving the same result?

Here's a typically example:

ParkingSpots *parkingSpots = [[[ParkingSpots alloc] initWithMapViewController:self] autorelease];
     keepRunning = YES;
     NSRunLoop *theRL = [NSRunLoop currentRunLoop];
     while (keepRunning && [theRL runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);

     UpdateLocation *updatedLocation = [[[UpdateLocation alloc] initWithUserid:@"me" andCoordinate:annotation.coordinate withMapViewController:self]
                autorelease];
     NSLog(@"Lat = %f, Long = %f",annotation.coordinate.latitude,annotation.coordinate.longitude);
     [updatedLocation sendUpdate];

In this code I need to wait until the parkingSpots object is completely initialized before I initialize updateLocation. Since updatelocation expects parkingSpots to be fully initialized, without the runloop updatedlocation was not initializing properly. WIth the runloop everything works as expected.

However this looks very ugly to me (setting a global var at various points in my code). Is there a more elegant solution? Thanks in advance for your help!

A: 

I think you need to look at objective-c's synchronization feature.

Eimantas
This just seems to protect a "critical region" of code from execution by more than 1 thread. What I need is to wait for one piece of code to complete (parkingSpots initialization) before another runs (updatedlocation initialization)
ennuikiller
then you should go with delegate design pattern.
Eimantas
+2  A: 

You could use the delegate pattern on your ParkingSpots class, and call the delegate when it finishes initialising. e.g.

ParkingSpots *parkingSpots = [[[ParkingSpots alloc] initWithMapViewController:self] autorelease];
parkingSpots.delegate = self;
parkingSpots.didInitialiseSelector = @selector(parkingSpotsInitialised:);
[parkingSpots startLengthyInitialisation];

- (void) parkingSpotsInitialised:(ParkingSpots*)parkingSpots {
  UpdateLocation *updatedLocation = [[[UpdateLocation alloc] initWithUserid:@"me" andCoordinate:annotation.coordinate withMapViewController:self] autorelease];
}

You could also use notifications to achieve the same thing.

Nathan de Vries
@Nathan thanks for the suggestion. I though about using notifications but was a little confused about exactly how to implement them. Do you think its possible to post some psuedocode to make it a bit clear? Thanks in advance!
ennuikiller
Thanks I went with nsnotifications and its working out great!!
ennuikiller