I use parse.com as backend, and my query was very slow, so I am trying to change it to use blocks.
Basically, my query populates an array with everything I need, and according to if statements, I'm calling methods inside the block, these methods populate the array that I will use in cellForRowAtIndexPath
. The problem is that when I try to reloadData
inside block, the app crashes.
Here is the code:
- (void)queryForTable {
PFQuery *exerciciosQuery = [PFQuery queryWithClassName:@"ExerciciosPeso"];
[exerciciosQuery whereKey:@"usuario" equalTo:[PFUser currentUser]];
[exerciciosQuery includeKey:@"exercicio"];
exerciciosQuery.cachePolicy = kPFCachePolicyCacheElseNetwork;
[exerciciosQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
[self configurarDatas];
_seriesArray = objects;
if (_seriesArray.count > 0) {
NSPredicate *predIniciante = [NSPredicate predicateWithFormat:@"serie contains [cd] %@", @"Ini"];
NSArray *arrayIniciante = [_seriesArray filteredArrayUsingPredicate:predIniciante];
NSArray *arrayInicianteApenasSeries = arrayIniciante;
NSArray *arrayInicianteApenasSeries2 = [arrayInicianteApenasSeries valueForKey:@"serie"];
NSSet *setInicianteApenasSeries = [NSSet setWithArray:arrayInicianteApenasSeries2];
NSArray *arrayInicianteCount = [setInicianteApenasSeries allObjects];
if (arrayInicianteCount.count > 0) {
[self popularSeriesInicianteAB];
// [self.tableView reloadData];
}
else if (arrayInicianteCount.count > 8) {
[self popularSeriesInicianteC];
// [self.tableView reloadData];
}
else {
[self popularSeriesAvancado];
// [self.tableView reloadData];
NSLog(@"POPULAR SERIES AVANÇADO");
}
}
}];
}
I have also tried to reloadData using dispatch_async(dispatch_get_main_queue(), ^{ [myDisplayedTable reloadData]; });
, but it didn't work as well.
Anyway, I imagine that if I erase my methods and put everything inside the block, it will work, but I don't want to do that, as calling methods using if
make my code easier to follow.
UPDATE:
For completeness, here are my DataSource methods:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"numberOfRowsInSection %li", (unsigned long)_seriesForDisplay.count);
return _seriesForDisplay.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = @"Cell";
PFTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[PFTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
PFObject *o = _seriesForDisplay[indexPath.row];
// Texto da célula
cell.textLabel.text = o[@"serieDisplay"];
cell.detailTextLabel.text = o[@"grupos"];
return cell;
}