views:

54

answers:

3

Hi everyone,

In my iphone app I have core data to-many relation topic -->> question, so every topic contains a few questions, I figured out how to display list of topics in UItableView using NSFetchedResultController, but I can't get how shall I construct my FetchedResultController to display questions in chosen topic.

I'm trying to find on Internet similar tutorial or example but can't find anything.

How can I do it?

Thank you very much

+2  A: 

You won't use an NSFetchedResultsController. Instead you'll define a property in your detail view controller

@property (nonatomic, retain) Topic *topic;

Then when you create your detail view controller, you'll set the property based on the topic selected in didSelectRowForIndexPath.

Then in your detail view it's just a matter of grabbing the questions out of the Topic object using the core data relationship (topic.questions)

DVG
Thanks for the answer, I kind of thought about it, but if my topic contains 1000 questions, app will hold all of them in memory, won't it? what the point of using NSFetchedResultsController at all? I thought using it I will be able to reduce memory usage. Or am I getting something wrong?
Burjua
besides in many questions here I saw discussions about using NSFetchedResultsController in detailsView, e.g. here: http://stackoverflow.com/questions/2931431/nsfetchedresultscontroller-on-secondary-uitableview-how-to-query-data or here: http://stackoverflow.com/questions/2218817/should-i-have-a-nsfetchedresultscontroller-in-every-view the only problem there is no explanation how to do it((
Burjua
A property is not needed, you can use KVC to access the relationship without the need of subclassing.
Marcus S. Zarra
A: 

Ok, I found out that I need to use NSPredicate for it. Now I'm trying to add it to my fetchedResultController like this:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"topic == %@", self.topic];
[fetchRequest setPredicate:predicate];

but getting an error:

'keypath topic not found in entity <NSSQLEntity QuestionItem id=2>'

How can I do it?

Thanks

Edit

Got it!

need to set up both side relation in data model and set predicate like this:

[NSPredicate predicateWithFormat:@"topic.topicName == %@", self.topic]

Burjua
This is incorrect. See my answer.
Marcus S. Zarra
+2  A: 

You do NOT need to go back to a NSFetchRequest to get the objects on the other end of a relationship. Just ask your Topic for its questions. This will give you a NSSet of entities that are faulted. Faulted entities take up VERY little memory and you can pull in 1000 of them easily.

If you need to filter those then you can perform a -filteredSetWithPredicate: on the NSSet that is returned but going out to disk is wasteful and slow.

Marcus S. Zarra
ok, thanks Marcus, in my rootViewcontroller I also need to add something like All topics cell and accordingly display list of all questions in detailsView. Is there any easy and low memory way to do it using this way?
Burjua
The memory is going to be as low as you design it. As long as you put all of the questions in a `NSFetchedResultsController` in that case, Core Data should keep the memory low. However, my suggestion in this case is to try it and see what happens. Pre-guessing memory issues is akin to pre-optimization; a sure waste of time.
Marcus S. Zarra
Sorry, I didn't get it, if you suggest that I should not use another `NSFetchedResultController` and should use NSSet instead, how can I get a set of all questions? thanks
Burjua
Your objects have a relationship. When a user taps on a row you get the topic associated with that row. You hand that topic off to the next `UIViewController` and push it onto the navigation stack. You then ask that topic, probably via [topic valueForKey:@"questions"] for a `NSSet` of the questions to display in the corresponding view.
Marcus S. Zarra
Sorry if I wasn't clear enought, but I need to get list (Set) of all questions in all topics, is it possible?
Burjua
For that one you would want to use a `NSFetchedResultsController` and treat it separately/differently than the rows that have a `Topic` selected. My answers were targeted at your original question where a `Topic` entity is selected. I would suggest creating new SO questions when you have a different question. Helps with clarity.
Marcus S. Zarra