Iphone 在应用程序的下一个操作之前完成请求操作

Iphone 在应用程序的下一个操作之前完成请求操作,iphone,ios,objective-c,Iphone,Ios,Objective C,我从数据库中获取一些数据,并将其保存到我创建的对象(称为“ManejadorMateria”)中。然后,我希望能够在下一个视图控制器的表视图中显示这些数据;问题在于,需要对上一个视图控制器的prepareforsegue方法执行请求操作,并且在将我需要的数据保存到对象ManejadorMateria之前,会调用表视图的cellForRowAtIndexPath方法 在触发CellForRowatineXpath方法之前,我可以做些什么来获取这些数据 要向数据库发出请求,我使用以下代码: -(vo

我从数据库中获取一些数据,并将其保存到我创建的对象(称为“ManejadorMateria”)中。然后,我希望能够在下一个视图控制器的表视图中显示这些数据;问题在于,需要对上一个视图控制器的prepareforsegue方法执行请求操作,并且在将我需要的数据保存到对象ManejadorMateria之前,会调用表视图的cellForRowAtIndexPath方法

在触发CellForRowatineXpath方法之前,我可以做些什么来获取这些数据

要向数据库发出请求,我使用以下代码:

-(void)commandWithParams:(NSMutableDictionary*)params onCompletion     (JSONResponseBlock)completionBlock
{
NSMutableURLRequest *apiRequest =
[self multipartFormRequestWithMethod:@"POST"
                                path:kAPIPath
                          parameters:params
           constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {

           }];

AFJSONRequestOperation* operation = [[AFJSONRequestOperation alloc] initWithRequest:   apiRequest];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    //success!
    completionBlock(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    //failure :(
    completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription]   forKey:@"error"]);
}];

[operation start];
}
- (void)cargarMateriasDeCarrera:(NSString *)carrera{

Materias = [[NSMutableArray alloc] init];
Manejador = [[ManejadorMateria alloc] init];

NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                               @"getMaterias", @"command",
                               carrera, @"carrera",
                               nil];


[[API sharedInstance] commandWithParams:params
                           onCompletion:^(NSDictionary *json) {
                               //handle the response
                               NSDictionary* res = [json objectForKey:@"result"];
                               Materia *materiafuente = [[Materia alloc]init];

                               for (id r in res) {
                                   materiafuente.Nombre = [NSString      stringWithFormat:@"%@",[r objectForKey:@"nombre"]];
                                   materiafuente.Codigo = [NSString stringWithFormat:@"%@",[r objectForKey:@"codigo"]];
                                   materiafuente.Descripcion = [NSString stringWithFormat:@"%@",[r objectForKey:@"descripcion"]];
                                   materiafuente.GradoDificultad = [r objectForKey:@"grado"];
                                   [Materias addObject:materiafuente];

                               }
                               [Manejador setMasterMateriasTodas:Materias];

                                }];


}
最后,在同一个视图控制器上的prepareforsegue上,我有:

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

usuario.Carrera = [carreras objectAtIndex:self.tableView.indexPathForSelectedRow.row];
TablaMateriasViewController3 *tbvc3 = [segue destinationViewController];
tbvc3.usuario = usuario;


NSString *command = @"registraCarrera";
NSMutableDictionary *params =[NSMutableDictionary dictionaryWithObjectsAndKeys:
                              command, @"command",
                              usuario.Carnet, @"carnet",
                              usuario.Carrera, @"carrera",
                              nil];

[[API sharedInstance] commandWithParams:params
                           onCompletion:^(NSDictionary *json) {
                           }];
[self cargarMateriasDeCarrera:usuario.Carrera];
tbvc3.Manejador = Manejador;
}
在触发CellForRowatingIndexPath方法之前,我可以做些什么来获取该数据

iOS是事件驱动的。你不能依赖事物的顺序。您不知道何时调用
cellforrowatinexpath
。因此,当调用
cellforrowatinexpath
时,要么您有数据,要么您没有数据。如果你没有,那么你提供一个填充物,或者什么都没有。如果是,则提供数据

如果您知道您有数据,并且希望再次调用
cellForRowAtIndexPath
,则调用
reloadData
或相关的
reload…
方法之一(例如
reloadrowsatinexpaths:withRowAnimation:
)。例如,当您获得一行的数据时,您可以在完成方法中调用它

在我的书中,我给出了一个示例,其中必须下载单元格每一行的图像。起初,每个图像都丢失了。但是当表视图请求
cellforrowatinexpath
时,图像丢失,我们开始下载该图像。当它到达时,我们刷新该行,因此再次调用
cellforrowatinexpath
,现在我们有了图像,因此我们提供了图像,它显示在表中:

完整的可下载示例项目:


您可能也可以采用相同的策略。

我希望使用线程,而不是调用该方法,因为我不知道异步下载(正如您正在做的)时所获得的数据的大小。谢谢matt,但是,当操作等待完成时,我如何使主线程休眠呢。读我在回答中说的话。看我书中的讨论。当操作完成时,您可以在主线程上重新加载表行。谢谢@matt,它使用[self.tableview reloadData]工作;