I need to pass 2 NSDictionary from a ViewController to another ViewController, My App some times pass the data ok, but, another times, data don't reach to second ViewController.
I need consult a web service twice, that web service response is a JSON, this JSON (2) is what I need pass to other ViewController
The call to web service is made here (in dispatch_async):
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t) (delayInSeconds * NSEC_PER_SEC));
dispatch_queue_t queue = dispatch_queue_create("com.micrologica.modules", nil);
dispatch_async(queue, ^{
urlConsultarDisponibilidad = [NSString stringWithFormat:connectionUrl getAvailableModuleListAPI, fechaDiaUno, idLocal, idServicioUno, idProfesionalUno, idServicioDos, idProfesionalDos];
[FunctionsAmano setConnectionAndRequest:urlConsultarDisponibilidad completion:^(NSDictionary *dataResponse) {
NSLog(@"response dia 1: %@", dataResponse);
if(dataResponse) {
NSString *resp = [NSString stringWithFormat:@"%@", [dataResponse objectForKey:@"resp"]];
if([resp isEqualToString:@"1"]) {
NSLog(@"almost finish 1");
modulesDiaUno = [dataResponse objectForKey:@"data"];
}
dispatch_after(popTime, queue, ^(void){
finishedDiaUno = YES;
NSLog(@"finished 1");
});
}
}];
});
dispatch_async(queue, ^{
urlConsultarDisponibilidad = [NSString stringWithFormat:connectionUrl getAvailableModuleListAPI, fechaDiaDos, idLocal, idServicioUno, idProfesionalUno, idServicioDos, idProfesionalDos];
[FunctionsAmano setConnectionAndRequest:urlConsultarDisponibilidad completion:^(NSDictionary *dataResponse) {
NSLog(@"response dia 2: %@", dataResponse);
if(dataResponse) {
NSString *resp = [NSString stringWithFormat:@"%@", [dataResponse objectForKey:@"resp"]];
if([resp isEqualToString:@"1"]) {
NSLog(@"almost finish 2");
modulesDiaDos = [dataResponse objectForKey:@"data"];
}
dispatch_after(popTime, queue, ^(void){
finishedDiaDos = YES;
NSLog(@"finished 2");
});
}
}];
});
dispatch_async(queue, ^{
dispatch_async(dispatch_get_main_queue(), ^{
BOOL exito = YES;
int segundos = 0;
/* Si la consulta se demora mas de 60 segundos, se interrumpe e informa al usuario de un problema */
while (!finishedDiaUno && !finishedDiaDos) {
[NSThread sleepForTimeInterval:1];
if(segundos >= 60) {
exito = NO;
break;
}
segundos++;
}
if(exito) {
HorariosViewController *horariosView = [self.storyboard instantiateViewControllerWithIdentifier:@"ModulesView"];
horariosView.modulesDiaUno = self.modulesDiaUno;
horariosView.modulesDiaDos = self.modulesDiaDos;
double delayInSeconds = 4.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t) (delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self presentViewController:horariosView animated:NO completion:nil];
});
}
});
});
This code is working fine, but, some times the response (dataResponse) comes after I make change of ViewController, I don't know why changes ViewController if the dataResponse not came.
How to you see, I instantiate of the second ViewController, I set the data, and change the ViewController on a dispatch_after (4.0 seconds). Why I change the ViewController in a dispatch_after? because, if I don't implement the dispatch_after, the NSDictionarys ALWAYS comes empty!, In this way, the NSDictionarys some times comes empty and some times comes ok.
Can you tell me why is wrong with my code?
PS: the dictionaries appear empties in the other ViewController (has no element, but is not null).