OscarTheGrouch
OscarTheGrouch

Reputation: 2384

NSFetchedResultsController fetching and displaying results combined from two entities iPhone SDK

Im am trying to improve a ToDo app for the iPhone.

I have two entities, 1. Task 2. Project

A project can have multiple tasks but a task may have none or one project.

I have been trying to display both in the same UITableView using NSFetchedResultsController

is it possible to fetch two entities and display/sort them both from the same UITableView?

Upvotes: 3

Views: 881

Answers (3)

Steve Weller
Steve Weller

Reputation: 4619

You need an abstract entity that is common to both. That will also let you define an attribute to make sorting into sections possible.

Upvotes: 0

lyonanderson
lyonanderson

Reputation: 2035

How are you modelling your data in Core Data? I would have thought you should have a Project entity that has one or more Task entities via a two way relationship.

You could then have a single NSFetchedResults controller that fetched all the Tasks. The Task entities will have a project member which my or may not be nil.

Within your table you would then be able to display the list of Tasks with their details and also details of the project e.g.

Task 1 - Do the shopping
Project: Home

Task 2 - Do the cleaning
Project: Home

etc etc

Upvotes: 0

Ian Henry
Ian Henry

Reputation: 22403

Not sure I understood completely, but you're trying to display Projects and Tasks in the same UITableView?

Off the top of my head I'd say you want to make an abstract entity, "Displayable" or something like that, and make it the parent of your Task and Project entities. (Assuming they display the same in your TableView). Then get all those and you'll have your full result set.

Note that I haven't tried this myself, but in theory it should do what you want.

Alternately, if that isn't an option, you could use two NSFetchedResultsControllers, one for each Entity, and just add some fancy conditional code to your rowForIndexPath: (or whatever) methods. Sorting might be a problem, though.

Upvotes: 1

Related Questions