Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top