Я застрял на том, как использовать groupBy с MagicalRecord.Как использовать groupBy с MagicalRecord?
У меня есть список стран с местами
Country -<< Venues
Мне нужно сгруппировать все места по стране и отсортировать страны по названию.
Но я не уверен, как это сделать с помощью MagicalRecord.
Я попытался использовать NSFetchedController
, но иногда он падает, говоря, что массив равен нулю или 0.
В других случаях он когда-либо видит только одну категорию, если ее несколько.
Наконец, я не уверен, как выполнить выборку на сущности.
ie;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
_objects = [NSMutableArray array];
self.fetchedResultsController = [self fetchedResultsController];
[Venue performFetch:self.fetchedResultsController];
// At this point how do I make the Venue findAllSortedBy work on the performFetch?
_objects = [NSMutableArray arrayWithArray:[Venue findAllSortedBy:@"name" ascending:YES inContext:[NSManagedObjectContext defaultContext]]];
self.title = @"Venues";
}
- (NSFetchedResultsController *)fetchedResultsController {
if (!fetchedResultsController) {
fetchedResultsController = [Venue fetchAllSortedBy:@"name"
ascending:YES
withPredicate:nil
groupBy:@"country"
delegate:self];
}
return fetchedResultsController;
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSLog(@"Section = %d", section);
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
NSLog(@"sectionInfo = %@", sectionInfo);
return @"Header";
}
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
Venue *v = [_objects objectAtIndex:indexPath.row];
cell.textLabel.text = v.name;
}
Я не уверен, что я делаю это правильно.
Вышеуказанное поместит все в 1 страну (если их несколько), и журнал сообщит;
CoreData: error: (NSFetchedResultsController) A section returned nil value for section name key path 'country'. Objects will be placed in unnamed section
Кажется, что не видно разных стран, и я не думаю, что правильно выполнил команду GroupBy.
Таким образом, как мне сделать команду GroupBy с помощью MagicalRecord?
Большое спасибо.
Отношения между страной и местом не сохранялись должным образом, но с вашей помощью это сейчас. Я не был уверен, должен ли я делать весь этот код или нет, но я счастлив, что его исправили, поскольку я все еще изучаю MagicalRecord – zardon