質問

グローバルなVARとNSRUNLOOPの組み合わせを使用して、アプリケーション全体で同期を強制します。うまくいきますが、私には少し醜いようです。同じ結果を達成する他の方法はありますか?

典型的な例は次のとおりです。

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

このコードでは、Updatelocationを初期化する前に、パーキングスポットオブジェクトが完全に初期化されるまで待つ必要があります。 updatelocationは駐車スポットが完全に初期化されることを期待しているため、Runloop UpdatedLocationが適切に初期化されていませんでした。 Runloopを使用すると、すべてが期待どおりに機能します。

しかし、これは私にとって非常にugいものです(私のコードのさまざまなポイントでグローバルなVARを設定します)。もっとエレガントなソリューションはありますか?よろしくお願いします!

役に立ちましたか?

解決

デリゲートパターンを使用できます ParkingSpots クラス、およびイニニカル化が終了したら、代表者に電話してください。例えば

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

通知を使用して同じことを達成することもできます。

他のヒント

Objective-Cのものを見る必要があると思います 同期機能.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top