Reputation: 198
I have a table that shows me the users that I am able to "ping". It should look something like this:
Best Friends (section name in tableview)
- User A - ping button
- User D - ping button
Friends (section name in tableview)
- User B - ping button
- User C - ping button
- User F - ping button
Pinged friends (section name in tableview)
- User E
- User G
The table is basically showing all my users (the same coredata entity), but in a grouped kind of way. I am using (or at least I think I should) nsfetchedresultscontroller because of the dynamics that should happen when you click on the Ping button in the first Section. The behaviour that I want is this:
1st section shows the "best friends" list that have not yet been pinged. The order is based on the number how many times you have pinged someone and their name (I already have a counter for that). This shows last X users.
in pseudo SQL : WHERE pinged = false AND numberOfPing >= 10 ORDER BY name ASC LIMIT 5
2nd section shows all my friends in alphanetical order that are not in the first or the last section.
in pseudo SQL : AND numberOfPing < 10 AND pinged = false ORDER BY name ASC
3rd section shows all my friends that have already been pinged.
in pseudo SQL : WHERE pinged = true ORDER BY name ASC
The questions:
Is the NSFetchedResultsController the correct way to go with this? I have had success with it so far at other tableviews and I like the "automatic" animations that it can produce.
I should then probably use only one fetchRequest, right? But I cannot find any suitable example that would come close to the behaviour I am looking for. Grouping based on these three criteria.
Any help is greatly appreciated.
Upvotes: 0
Views: 72
Reputation: 119031
The FRC doesn't produce the animations, 'all' it does (in the context of what we're discussing) is to observe the managed object context for changes and tell you (as the delegate) what has happened. It's the delegate method implementation which tells the table view what animations to perform. As such, the FRC isn't required to achieve the animations you want.
As you say, the way you want to sort and the sections you will have are hard to achieve with an FRC. And the user action which triggers the animation is via a button that your controller would be handling anyway. So it's probably best to not use an FRC in this case.
You could use different FRCs for each section of the table. This would only really be beneficial if you expect any of the sections to have a lot of rows (so you can take advantage of the FRC pagination).
Upvotes: 1
Reputation: 80265
One aspect that should make this quite easy is the fact that each User
instance should know through introspection to which category it belongs.
You can implement a method in your User
NSManagedObject
subclass (tip: use a category) that returns the section category (it can be numeric, so you can sort by it). You can use this computed category as the sectionNameKeyPath
for your fetched results controller.
See Apple's DateSectionTitles Sample Code for how to implement this in the managed objects. They use a transient property, which is the most robust way to do this.
Upvotes: 1