[英]Grouping three entities with many-to-many relationships
在過去的幾天里,我一直在嘗試找出如何實現這一目標的方法,而我所掌握的知識卻是不可能的。
我設計了三個實體List <<->> Item <<->>存儲在核心數據模型設計器中。 它們每個都只有一個稱為“名稱”的屬性。
目標是選擇一個列表,然后顯示按商店分組的列表中的所有項目。
我嘗試使用:
// Set entity.
entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set object filters.
predicate = [NSPredicate predicateWithFormat:@"ANY list.name == %@", self.list.name];
[fetchRequest setPredicate:predicate];
// Set FRC
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"store.name" cacheName:nil];
錯誤:
'Invalid to many relationship in setPropertiesToFetch: (store.name)'
而且也可以通過這種方式手動填充行(我還不知道如何):
// Set entity.
entity = [NSEntityDescription entityForName:@"Store" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set object filters.
predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(item.list, $x, $x.name == %@).@count > 0", self.list.name];
[fetchRequest setPredicate:predicate];
// Set FRC
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"name" cacheName:nil];
錯誤:
'Only allowed one toMany/manyToMany relationship in subquery expression collection (SUBQUERY(item.list, $x, $x.name == "List02"))'
並且還嘗試了“獲取的屬性”和其他無處可去的方法。
有任何想法嗎?
問候。 佩德羅。
好的,這是解決方案(danypata和Martin R給我鑰匙)。
為此,您應該添加一個新實體以打破多對多關系。 最終的核心數據模型是:列表<<->> Item <->> ItemStore <<-> Store。 “ ItemStore”實體不需要具有任何屬性,只需具有關系即可。
編碼...
- (NSFetchedResultsController *)fetchedResultsController
{
[...]
// Set entity.
entity = [NSEntityDescription entityForName:@"ItemStore" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set object filters.
predicate = [NSPredicate predicateWithFormat:@"ANY item.list.name == %@", self.list.name];
[fetchRequest setPredicate:predicate];
// Edit the sort key as appropriate.
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"item.name" ascending:YES];
sortDescriptors = @[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Set FRC.
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"store.name" cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
[...]
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
Item *item;
ItemStore *itemStore;
[...]
itemStore = (ItemStore *)[self.fetchedResultsController objectAtIndexPath:indexPath];
item = itemStore.item;
cell.textLabel.text = item.name;
[...]
}
最后說明:
謝謝大家 佩德羅。
如前所述,如果一個項目可以在一個以上的商店中,則不能按商店提取項目和分組。 但是,在表視圖或類似視圖中使用NSFetchedResultsController
進行顯示仍然可以(並且很常見)。
只需獲取要分組的實體,在本例中為Store
。 相應地調整表視圖datasource
方法:
節數:
return _fetchedResultsController.fetchedObjects.count
本節標題:
Store *store = _fetchedResultsController.fetchedObjects[indexPath.section];
return store.name;
部分中的行數:
Store *store = _fetchedResultsController.fetchedObjects[indexPath.section];
return store.items.count;
商店中的一項:
Store *store = _fetchedResultsController.fetchedObjects[indexPath.section];
Item *item = [[store.items sortedArrayUsingSortDescriptors:@[
[NSSortDescriptor sortDescriptorWithKey:@"sortAttribute" ascending:YES]]
objectAtIndex:indexPath.row];
// configure cell with information from item.
因此,盡管這是可能的,但也許您仍然想重新考慮數據模型。 一個商品在多個商店中真的有意義嗎? 如果不是,則可以使用普通的香草提取結果控制器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.