views:

131

answers:

4

Hello,

I am new to working with Core Data, and am working with a UITableView. I have a toolbar with a UISegmentedController, and I want to filter the items in the UITableView based on the selected index. In other words, suppose I have a UITableView that displays Books (stored in Core Data) and a UISegmentedController with segments to display books in "English", "Spanish", and "French".

What is the approach here to get everything hooked up? When one of the segments is clicked, what do I do in the UISegmentedControl's target to change things around?

Sorry if it's a stupid question!

A: 

Its a good idea to use three different Arrays for each of your filters. Cache them somewhere so their is no delay when the user selects a filter. To find the information you are looking for from your CoreData store use NSPredicate.

ChinaPaul
Using arrays with Core Data is more code than you need to write. Better to use a `NSFetchedResultsController` as that is what it is designed for.
Marcus S. Zarra
duly noted: I especially like that NSFetchedResultsController uses some kind of caching. Most of my data is dynamic so I guess I think in NSMutableArrays for most things
ChinaPaul
Even with dynamic data, Core Data with an In-Memory store plus a `NSFetchedResultsController` is going to be less code and better performance in nearly every situation. Food for thought.
Marcus S. Zarra
A: 

You can use NSFetchedResultsController, when you clicked on segment just set the different perdicate and perform fetch again.

jamapag
Where can I find sample code for this approach?
Jake Hansen
Read: http://developer.apple.com/iphone/library/documentation/CoreData/Reference/NSFetchedResultsController_Class/Reference/Reference.html for creating and using NSFetchedResultsController.
jamapag
If you only use one then you lose the advantage of its cache. Better to use one for each segment.
Marcus S. Zarra
+1  A: 

I would use a separate NSFetchedResultsController for each segment. This will allow you to take advantage of the built in cache for each segment and improve performance.

In addition to Apple's documentation (and my book), you can also read up on them from my article Touching The Core in the PragPub magazine.

Marcus S. Zarra
I can't help but say that as you someone that already gives so much to the community, I am absolutely astonished that you take the time to answer questions like mine on SO. A million thank you's!
Jake Hansen
Marcus, what would be the cleanest way to implement this? If you had three segments, this would require three FRC's, and it wouldn't be very pretty to have all those `switch() case:` statements in the tableview code. Perhaps create a `typedef enum` with labels corresponding to the individual segments then add the FRC's to an NSArray in the class at the appropriate indices? Then all you would have to do is reference something like `[self.frcArray objectAtIndex:[segControl selectedSegmentIndex]]` where there would otherwise be a `switch() case`. Or is there a cleaner way to do it?
Neal L
have them as iVars and have a `currentFRC` iVar. When the segmented control is tapped, you switch what `currentFRC` is pointing to and tell the tableView to reload. Everything else talks to the `currentFRC` and have checks in your delegate methods so they ignore any messages that are not from the `currentFRC`.
Marcus S. Zarra
A: 

Hi Guys

Slightly related to the above..

Is there a way to animate the switching/filtering of the views much like the accordian type animation that is used on the "recents" tab of the phone app?

I've been looking into adding and deleting the cells in and out but can't get the same effect. Can anyone help or point me in the right direction?

Phil Patch