문제

글로벌 VARS와 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을 초기화하기 전에 Parkingspots 객체가 완전히 초기화 될 때까지 기다려야합니다. updatelocation은 주차 스팟이 완전히 초기화 될 것으로 예상하기 때문에 런 루프 업데이트로 위치가 제대로 초기화되지 않았습니다. 런 루프를 사용하면 모든 것이 예상대로 작동합니다.

그러나 이것은 나에게 매우 못 생겼다 (내 코드의 다양한 지점에서 글로벌 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