Question

Je me retrouve avec une combinaison de vars globales et NSRunLoop pour forcer la synchronisation dans toute ma demande. Bien que cela fonctionne, il semble un peu laid pour moi. Est-il un autre moyen d'atteindre le même résultat?

Voici un exemple typique:

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];

Dans ce code, je dois attendre jusqu'à ce que l'objet parkingSpots est complètement initialisé avant que j'initialiser updateLocation. Depuis updateLocation attend parkingSpots soit complètement initialisé, sans updatedlocation de runloop n'a pas été correctement initialise. Avec le tout runloop fonctionne comme prévu.

Cependant, cela semble très laid pour moi (définir une variable globale à différents points dans mon code). Y at-il une solution plus élégante? Merci d'avance pour votre aide!

Était-ce utile?

La solution

Vous pouvez utiliser le modèle de délégué de votre classe ParkingSpots et appeler le délégué quand il se termine initialisant. par exemple.

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];
}

Vous pouvez également utiliser les notifications pour obtenir la même chose.

Autres conseils

Je pense que vous avez besoin de regarder de Objective-C fonction de synchronisation .

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top