I am trying to use the new sendAsynchronousRequest added in iOS5. I have a model class (File) with a method that requests some data from the server, and then passes this data to the controller class (FilesController) that created the model object.
The model class has a method with the following code:
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSArray *decodedResponse = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSArray *files = [self _dictionariesToFiles:decodedResponse];
handler(files);
}];
The controller class uses it as follows:
[File findAll:conditions completionHandler:^(NSArray *files) {
dataSource = files;
NSLog(@"set");
[self.tableView reloadData];
NSLog(@"reload");
activityIndicator.hidden = TRUE;
}];
In the console, I can immediately see how NSLogs displays the “set” and “reload” messages, but the table and indicator do not change until a few seconds have passed (5-10 seconds).
Does anyone know where the problem is?
Thank.
PD: I changed the async request to compatible synchronized, and then the problem went away, but you want to use the async request for this.
:
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSArray *decodedResponse = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
NSArray *files = [self _dictionariesToFiles:decodedResponse];
handler(files);