我陷入困境,如果不进行同步网络通话,我似乎无法进步。这是代码:
__block NSInteger duration = 0;
NSInteger index = [pathPoints indexOfObject:@(minutes)
inSortedRange:NSMakeRange(0, pathPoints.count)
options:NSBinarySearchingFirstEqual | NSBinarySearchingInsertionIndex usingComparator:^NSComparisonResult(id obj1, id obj2) {
if ([obj1 class] == [CLLocation class]) {
[OTGNetworkRequest fetchDistanceBetweenStart:self.startLocation andEnd:(CLLocation *)obj1 success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *json = responseObject;
NSDictionary *element = json[@"rows"][0][@"elements"][0];
duration = [element[@"duration"][@"value"] intValue];
}];
NSInteger seconds = [obj2 intValue] * 60;
return [@(duration) compare:@(seconds)];
} else {
[OTGNetworkRequest fetchDistanceBetweenStart:self.startLocation andEnd:(CLLocation *)obj2 success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *json = responseObject;
NSDictionary *element = json[@"rows"][0][@"elements"][0];
duration = [element[@"duration"][@"value"] intValue];
}];
NSInteger seconds = [obj1 intValue] * 60;
return [@(seconds) compare:@(duration)];
}
}];
所以,问题是我正在尝试设置持续时间,这需要对Google Distance API进行网络调用。但是,当我返回NSComparisonResult时,还没有设置持续时间。在返回之前,如何确保正确设置持续时间?我不能把它放在成功回调中,因为成功的回调块不应该返回任何东西。
我是否被迫使用同步网络电话,或者我没有通过正确的方式解决这个问题?