NSDictionary数据不会传递给另一个ViewController

时间:2014-03-28 00:57:11

标签: ios uiviewcontroller nsdictionary

我需要将2个NSDictionary从ViewController传递到另一个ViewController,My App有时会将数据传递给ok,但是,另一次,数据没有到达第二个ViewController。

我需要两次咨询一个Web服务,那个Web服务响应是一个JSON,这个JSON(2)是我需要传递给其他ViewController的

此处调用Web服务(在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];
            });
        }
    });
});

这段代码工作正常,但是,有些时候响应(dataResponse)是在我更改ViewController之后,我不知道为什么更改ViewController,如果dataResponse没有来。

如何看,我实例化了第二个ViewController,我设置了数据,并在dispatch_after(4.0秒)上更改了ViewController。 为什么我在dispatch_after中更改ViewController?因为,如果我没有实现dispatch_after,NSDictionarys总是空的!这样,NSDictionarys有时会变空,有些时候会好的。

你能告诉我为什么我的代码有问题吗?

PS:字典在其他ViewController中显示为空(没有元素,但不是null)。

1 个答案:

答案 0 :(得分:0)

这是一个建议:不要阻止你的主线程,不要在主线程上调用sleep。

这是一个并发问题,方法setConnectionAndRequest:urlConsultarDisponibilidad应该是异步的,UI代码可以在两个响应之前执行。我认为可以这样解决:

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");

                if (finishedDiaDos) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        HorariosViewController *horariosView = [self.storyboard instantiateViewControllerWithIdentifier:@"ModulesView"];
                        horariosView.modulesDiaUno = self.modulesDiaUno;
                        horariosView.modulesDiaDos = self.modulesDiaDos;
                        [self presentViewController:horariosView animated:NO completion:nil];
                    });
                }
            });
        }
    }];
});

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");

                if (finishedDiaUno) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        HorariosViewController *horariosView = [self.storyboard instantiateViewControllerWithIdentifier:@"ModulesView"];
                        horariosView.modulesDiaUno = self.modulesDiaUno;
                        horariosView.modulesDiaDos = self.modulesDiaDos;
                        [self presentViewController:horariosView animated:NO completion:nil];
                    });
                }
            });
        }
    }];
});