2016-06-23 4 views
0

내가 아이 패드 아이폰 OS 8 응용 프로그램에서 일하고 있어요, 나는 내 애플로부터 응답을 기다립니다해야 :3 "for"루프가 비동기 응답을 기다립니다. OBJ-C IOS

[directions calculateETAWithCompletionHandler:^(MKETAResponse *response, NSError *error) {}] 

이 방법은 내부에 3 개 루프입니다. 나는 dispatch_semaphore_t을 시도했지만 응용 프로그램이 줄 끝에서 계속할 수 없습니다 :

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); 

및 내동댕이. 내가 dispatch_group_t와 함께 시도하고 같은 결과를 얻었다. 나는 뭔가 잘못하고 있다고 생각하지만, 나는 무엇을 모른다. 나는 비슷한 문제를 찾아 SO를 시도했지만 아무 것도 발견하지 못했습니다. 누군가 내가 이것을 성취 할 수있는 방법을 설명 할 수 있습니까?

-(void)setTimesMissions { 

for (Driver *d in self.dataList) { 

    for (Period *p in d.periods) { 

      for (Mission *m in p.missions) { 

       MKDirections *directions = .... 

        // HERE i want the for loop stop until this completionHandler finish 
        [directions calculateETAWithCompletionHandler:^(MKETAResponse *response, NSError *error) { 

         //and when he finish here continue 
        }]; 
      } 
     } 
} 

}

+1

루핑 및 스레딩에 영향을주는에만 관련 부분에 코드 샘플을 아래로 껍질 벗기기 고려 루프에 dispatch_semaphore_wait를 사용합니다. 거기에 여분의 "보풀"은 당신이 무엇을 요구하는지 정확하게 말하기 어렵게 만듭니다. – Stonz2

+2

잘못된 길을 기다리고 있습니다. 앱을 대기 상태로 유지하려면 UI에 활동 표시기 ** 스레드 차단 안함 **을 표시합니다. 완료 처리기를 사용하여 작업이 완료 될 때 수행해야하는 모든 작업을 수행하십시오. – Sulthan

+0

나는 여전히 노력했지만 아무 것도 시도하지 않았다. – NLU

답변

1

는 dispatch_async 블록에서 메서드를 호출합니다.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    [youClassInstance setTimesMissions]; 
}); 

그리고

- (void)setTimesMissions { 
    Mission *home = [[Mission alloc]init]; 
    Mission *next = [[Mission alloc]init]; 
    for (Driver *d in self.dataList) { 

     home.clientLat = d.centralPointLat; 
     home.clientLon = d.centralPointLon; 
     home.clientPaddres = d.centralAddress; 

     for (Period *p in d.periods) { 

      home.times = [[NSMutableArray alloc]init]; 

      if ([p.periodIx isEqualToString:self.thisPeriodIX]) { 

       for (Mission *m in p.missions) { 

        Mission *source = home; 
        Mission *destination = m ; 
        MKPlacemark *placemarkSource = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake([source.clientLat doubleValue], [source.clientLon doubleValue]) addressDictionary:nil] ; 
        MKMapItem *mapItemSource = [[MKMapItem alloc] initWithPlacemark:placemarkSource]; 

        MKPlacemark *placemarkDestination = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake([destination.clientLat doubleValue], [destination.clientLon doubleValue])addressDictionary:nil] ; 
        MKMapItem *mapItemDestination = [[MKMapItem alloc] initWithPlacemark:placemarkDestination]; 

        MKDirectionsRequest *directionsRequest = [[MKDirectionsRequest alloc] init]; 
        [directionsRequest setSource:mapItemSource]; 
        [directionsRequest setDestination:mapItemDestination]; 
        directionsRequest.transportType = MKDirectionsTransportTypeAutomobile; 
        [directionsRequest setRequestsAlternateRoutes:NO]; 
        MKDirections *directions = [[MKDirections alloc] initWithRequest:directionsRequest]; 

        dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); 
        __block double timeTo; 

        [directions calculateETAWithCompletionHandler:^(MKETAResponse *response, NSError *error) { 

         if (response.expectedTravelTime) { 
          timeTo = response.expectedTravelTime; 
          double ans = timeTo; 
          Time *t = [[Time alloc]init]; 
          t.ix = m.serviceIX; 
          t.time = ans; 
          [home.times addObject:t]; 
         } 

         dispatch_semaphore_signal(semaphore); 
        }]; 

        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); 
       } 

       dispatch_async(dispatch_get_main_queue(), ^{ 
         // code that should be executed on main queue 
       }); 


       if (next.clientPaddres) { 
        home = next; 
       } 
      } 
     }   
    } 
} 
+0

감사합니다! 잘 작동한다. – NLU

관련 문제