Search code examples
ioscocoa-touchcore-dataiphonecoredatarecipes

CoreData - Modeling mapping table


Im planning on using CoreData to model a “News” iOS app. This’ll have many sections (read a UITabBarItem) which will contain many articles (in a UITableView). The articles might appear in other sections too. Also the sort order of each article is another important factor.

In database lingo I'd have

  • "Article" table
  • Sections" table and a
  • "Article_Sections" table to map an article to a section with additional sort order field.

I have tried and can’t model this in CoreData. Im stuck with a

  • “Section” class having a many-to-many relationship with
  • a “Article” class (with an inverse many-to-many relationship to the “Section” class).

However with this I don’t know how to derive the sort order. Any hint on how to do this is very much appreciated. Thanks in advance.

Sandeep


Solution

  • I had to stop thinking in terms of database to sort this out. My solution involved 2 Classes:

    • ArticleList

    Properties: * List name * Article Id * Sort order

    Relationship: * Article ; one-to-one

    • Article

    Properties: * Article Id * ....

    Relationship: * List ; one-to-many

    I query the ArticleList and sort on sortOrder to get a list of articles for a particular feed/section. Then use the valueForKeyPath: to fetch article properties (such as title etc).