1
Scenario, I have a data request using JSON but I would like this request to be performed outside the main thread of the app, so that it does not catch and that the user can perform other operations while the data is downloaded.
In the code below I can already download the data normally, but I only need it to be done in a secondary thread.
-(void)recebeTodosOsDadosPorFuncionarioDoWebService{
NSMutableString* strURL =[[NSMutableString alloc]initWithFormat:@"http://localhost/advphp/ArquivosFuncionando/pegaDadosNaoLidosPorFuncionario.php?funcionario=%@",funcionarioCadastrado];
NSURL* url = [NSURL URLWithString: strURL];
NSData* data = [NSData dataWithContentsOfURL:url];
NSError*erro;
arraySalvaDadosProcessos = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&erro];
if(arraySalvaDados == (id)[NSNull null]|| [arraySalvaDados count] == 0){
NSLog(@"Erro para receber dados do webservice: \n\n%@", arraySalvaDados);
}else{
[self salvaTodosOsDadosRecebidosNoBancoLocal];
}
}
I pointed out the solution of @Rafaelleão as the correct one because it is practical, but yours is also viable and good! Thank you very much!
– Tiago Amaral
@Tiagoamaral without problems, by programming best practice is you use a class
delegate
to make the requests and get the answers. Unless your case is something very specific somewhere in your project.– Gian