开发者

Errors when using NSFetchedResultsController with 3 related CoreData items

开发者 https://www.devze.com 2023-01-07 23:05 出处:网络
I\'m programming an app for an restaurant and want to show the restaurants menu in a sectioned table view. Above the table view I\'ve got a UISegmentedControl which controls which Menu Section to disp

I'm programming an app for an restaurant and want to show the restaurants menu in a sectioned table view. Above the table view I've got a UISegmentedControl which controls which Menu Section to display (Food or wine). The table should show the dishes separated by sections (pe. starters, meat, etc).

I've got 3 related entities MenuSections, MenuSubsections and MenuItems which are updated from a server (items change frequently) so I wanted to display the saved data and reload if the background process has downloaded any changes.

So far so good, but now I'm facing some problems when trying to retrieve the data.

  • When initializing the controller I need to define "sectionNameKeyPath" which is in MenuSections.menuSubsection.name, but I get an error saying "to-many key not allowed here".
  • I would like to sort both MenuSubsections and MenuItems, but again an error "to-many key not allowed here".

Can anyone shed some light on this as I were not able to find any info :-(

Thanks in advance, Miguel

UPDATE:

Items created in XCDataModel:

MenuSections: 
id (int16)
name (string)
modified (date)
menuSubsections (relation to-many with MenuSubsections)

MenuSubsections:
id (int16)
name (string)
modified (date)
menuSections (relation to-one with MenuSections)
menuItems (relation开发者_Go百科 to-many with MenuItems)

MenuItems:
name (string)
modified (date)
price (decimal)
menuSubsections (relation to-one with MenuSubsections)


You can't have a to-many relationship in a section name key path because the fetched results controller has no idea which of the many attribute values goes with which section title.

Suppose each MenuSections object has a relationship with 10 MenuSubsections objects each of which in turn has a relationship with 10 MenuItems objects. That is 100 MenuItems.name attributes to choose from. Which one should the controller select for which section?

You need two tables here. The first displays MenuSections objects and uses MenuSections.name as the section name key path. The rows are each MenuSections' related MenuSubsections objects. Selecting a row should push a detail table showing that row's particular MenuSubsections object's related MenuItems.

BTW, by convention, entity names are singular. So MenuSection, MenuSubsection and MenuItem. Entities are like class definitions. There is only one of them just as there are many NSString objects but only one NSString class so we don't name the class NSStrings. The name should reflect the singular nature. Plurals should be researved for to-many relationship names.

0

精彩评论

暂无评论...
验证码 换一张
取 消