2016-06-23 13 views
0

私はiPadのiOSの8のためのアプリに取り組んでいる、と私は私のアプリからの回答を待つ行う必要があります。このメソッドは、3つのループの内側にある"for"ループは非同期応答を待つ。 OBJ-CのIOS

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

。私は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

関連する問題