Core Data, UITableView, and UISegmentedControl - iphone

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!

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.

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.

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

I have implemented this as follows using the guidelines from Marcus above(I'm new to this so it may not be the best approach). I have a segment controller with three options for 'open', 'in progress' and 'closed'.
In the ViewController.h, create an iVar for each one of your segment options, and one iVar for the main controller that will store the current controller.
#property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
#property (nonatomic, retain) NSFetchedResultsController *inprogressFetchedResultsController;
#property (nonatomic, retain) NSFetchedResultsController *openFetchedResultsController;
#property (nonatomic, retain) NSFetchedResultsController *closedFetchedResultsController;
In the ViewController.m you need to create methods for lazy loading of these controllers, so I have three in total. They are basically the same except for the predicate and the cacheName, I have only shown one below.
- (NSFetchedResultsController *)closedFetchedResultsController
{
if (_closedFetchedResultsController != nil) {
return _closedFetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Ticket" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"priority.name" ascending:NO];
NSArray *sortDescriptors = #[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"status = 'Closed'"];
[fetchRequest setPredicate:predicate];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:#"priority.name" cacheName:#"ClosedTickets"];
aFetchedResultsController.delegate = self;
self.closedFetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.closedFetchedResultsController performFetch:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
return _closedFetchedResultsController;
}
Create an IBAction for your segment so that when it is changed it changes the fetched results controller and reloads the table.
- (IBAction)statusChanged:(id)sender {
switch (self.segmentControl.selectedSegmentIndex) {
case 0:
self.fetchedResultsController = self.inprogressFetchedResultsController;
break;
case 1:
self.fetchedResultsController = self.openFetchedResultsController;
break;
case 2:
self.fetchedResultsController = self.closedFetchedResultsController;
break;
default:
break;
}
[self.tableView reloadData];
}
That's it!
N.B. I also added this line to my ViewDidLoad method so that it would load the correct option into the fetchedResultsController initially.
self.fetchedResultsController = self.inprogressFetchedResultsController;

Related

Count Distinct items in Core Data

I have a Core Data entity with a property named 'value' which is often repeated. I wish to retrieve only unique values (done) and also how often each one appears, so that I can sort by that property (I'm building an autocomplete function based on existing user input, so knowing how frequently a certain input has appeared is essential).
My fetch request currently looks like this:
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Item" inManagedObjectContext:self.managedObjectContext];
NSDictionary *entityProperties = [entity propertiesByName];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:10];
[fetchRequest setFetchLimit:20];
[fetchRequest setReturnsDistinctResults:YES];
[fetchRequest setResultType:NSDictionaryResultType];
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObject:[entityProperties objectForKey:#"value"]]];
NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:#"value" ascending:YES] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"value BEGINSWITH[c] %#",predicateString];
[fetchRequest setPredicate:predicate];
return fetchRequest;
I'm a bit stuck with this one. Any ideas?
Thanks in advance!
I don't like this one, but...
You could do an other fetchRequest with a predicate that exactly match the value you are seeking. Allowing duplicate, then have the count of the array.
This one is better, but more work up front.
An other way could be to have a derived property in your data model that keep track of your duplicated count as you create them.
(with that option you could easily sort by duplicated count)
Ok for a Derived property.
First you will need to subclass NSManagedObject and use that subclass in your data model. (in Xcode 3 there was a way to create that quickly, but I don't know that in Xcode 4) But if you name it the same as your entity I think core data will pick it up.
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#interface Person : NSManagedObject
#property (nonatomic, retain) NSString * name;
#property (nonatomic, retain) NSString * firstLetterOfName;
#property (nonatomic, retain) NSString * phoneNumber;
#end
And in your implementation you will need to do something like this (there is code snippet in Xcode 4 to get you started, but there is a typo in one of them, unless it have been corrected)
#import "Person.h"
#implementation Person
#dynamic name, phoneNumber, firstLetterOfName;
- (void)setName:(NSString *)value {
[self willChangeValueForKey:#"name"];
[self setPrimitiveValue:value forKey:#"name"];
self.firstLetterOfName = [value substringToIndex:1];
[self didChangeValueForKey:#"name"];
}
#end
You can see that the firstLetterOfName is set each time the Name is set.
You can do the same kind of thing with relationship.
So when you add an item to a relationship you should be able to look up your relationship.
Here is an exemple of something similar, where I need to find if the object I'm adding have the lowest price of it's group because of a derived property call isMeilleurPrixAvecPrixElment. (this is old code, so I don't recall every detail of it, it have been done in OSX.4)
- (void)addPrixHistoriqueObject:(PrixElement_MO *)value
{
NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
[self willChangeValueForKey:#"prixHistorique" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
if ([self isPrixRegulierAvecPrixElement:value])
[self enleveL_AutrePrixRegulierPourCommerceDeCePrixElement:value];
if ([self isMeilleurPrixAvecPrixElment:value])
[self echangeMeilleurPrixAvecCePrixElement:value];
[[self primitivePrixHistorique] addObject:value];
[self didChangeValueForKey:#"prixHistorique" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
[changedObjects release];
}
In respond to a comment
depending on your data model and the importance of that aspect in your application, I can think of 3 solutions.
1- Redesing your data model around that aspect.
2- When setting the values query the rest of your entity with a predicate and update a property that have the count.
3- (I'm not sure of that one, but is worth trying) NSManagedObect is an object, so maybe you could have a static dictionary that have the value as a key and the count as value.
I would maybe try number 3 first (it look like the easy one), but I've never done something like that. So I'm not sure for the presitance of a class variable in core data.

NSFetchResultsController w/ NSPredicate (relationship) vs. Basic NSFetchRequest (Pic Included)

Here is an image of the relationship I am debating:
I have a UIScrollView setup as a horizontal scroller that scrolls between 3 different UIViewControllers (containing a UITableView of course and the required delegate methods, etc.)
Each ViewController in the UIScrollView loads a UITableView of a specific MyObjectType.
(E.g. ViewController1 loads a tableview of all MyObjects where its type == MyObjectType.name)
Does this make sense? You'll notice I've setup an inverse relationship between the objects. A MyObjectType can have many MyObject's but a MyObject can only have a single MyObjectType associated to it.
When I first load one of the UIScrollView viewController's I need to determine what MyObjectType this UITableView is for. I have this working fine and I set the Table Header accordingly.
E.g. [type valueForKey:#"name"] where type is a fetched result NSManagedObject of MyObjectType.
The thing is I'm wondering, when I obtain this NSManagedObject of MyObjectType do I not also have access to a NSSet *array (ie. [type valueForKey:#"objects"]) which I can use as the UITableView's datasource? Would this work if after I add or delete an object I save the managedContext and then I always [tableView reloadData] ?
I'm guessing this would work, as long as I don't require the UITableView content to change and update dynamically as new MyObject of this type are added? For this we require a NSFetchedResultsController right?
Here is my code for loading ALL MyObject's into a UITableView (which works):
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:#"MyObject" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc]
initWithKey:#"creationDate" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext sectionNameKeyPath:#"transientSectionDate"
cacheName:#"Root"];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
[sort release];
[fetchRequest release];
[theFetchedResultsController release];
return _fetchedResultsController;
}
Could someone PLEASE be as so kind to show my what actual NSPredicate declaration I need to correctly load ONLY MyObject's whose MyObjectType.name == #"XXXXXX"? Let's assume I already have a MyObjectType.name stored in a retained NSString inside the ViewController.
Thanks in advance!
The predicate format string would be:
#"ALL type.name=%#", typeName
However, since you do have a particular MyObjectType object, you already have direct access to the needed MyObject objects and don't have to waste time trying to fetch them. Just convert the set into a sorted array.
To keep apprised of ongoing changes while the table is active, implement observeValueForKeyPath:ofObject:change:context: in the tableview datasource object. Then send addObserver:forKeyPath:options:context: to that particular MyObjectType object like so:
[anObjectType addObserver:self
forKeyPath:#"objects"
options:(NSKeyValueObservingOptionNew |NSKeyValueObservingOptionOld)
context:nil];
Now, whenever the objects value of that paticular MyObjectType changes, the tableview's datasource will be notified and can change the table.
See Key-Value Observing Programming Guide for details.

Add a UISegmentedController to navigation bar with NSFetchedResultsController

I am working with a UITableView that gets its data from an NSFetchedResultsController. I would like to add a UISegmentedControl to my navigation bar that would toggle the table between displaying all of the records and only the records where starred == YES.
I have read some other SO posts indicating that one way to do this is to create a second NSFetchedResultsController that has an NSPredicate with starred == YES, but it seems awfully overkill to create a second NSFetchedResultsController.
Is there a simpler way to do this?
Not according to the docs on NSFetchedResultsController. If you take a look at the documentation on the fetchRequests property, you'll see the following note:
Important: You must not modify the
fetch request. For example, you must
not change its predicate or the sort
orderings.
Since the fetchRequest property is read-only, the only option is creating a new fetched results controller.
It might be possible to change the predicate and have things work, but it's generally a bad idea to do stuff that goes explicitly against things Apple says in the documentation, because it could break in a future release.
And, beware of premature optimization! Unless you've tested it out and found out that creating a whole new fetched results controller is a big performance drain, it's not worth trying to do something in a non-recommended way.
Here's how I set a new predicate on my fetched results controller. fetchedResultsController is a property of my view controller. predicate is a private ivar of the view controller.
I already had all of the code for creating the fetched results controller on demand, so to set the predicate it's just a matter of deleted the cached one.
- (void)setPredicate:(NSPredicate *)newPredicate {
predicate = [newPredicate copy];
// Make sure to delete the cache
// (using the name from when you created the fetched results controller)
[NSFetchedResultsController deleteCacheWithName:#"Root"];
// Delete the old fetched results controller
self.fetchedResultsController = nil;
// TODO: Handle error!
// This will cause the fetched results controller to be created
// with the new predicate
[self.fetchedResultsController performFetch:nil];
[self.tableView reloadData];
}
This code if based on the boilerplate XCode generates when you start a project that uses Core Data.
- (NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController != nil) {
return fetchedResultsController;
}
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:[MyEntity entityName] inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setPredicate:predicate];
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:#"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];
return fetchedResultsController;
}

UITableView not updating DataSource after change to NSFetchedResultsController

I have an UITableView populated by a NSFetchedResultsController. The initial fetch works fine. I can add, remove, modify, etc with zero problems.. But I want to add user-defined sorting to the table. I am doing this by changing the NSFetchedResultsController to use a different sortDescriptor set, and a different sectionNameKeyPath. Here is the code where I change the fetch:
-(void)changeFetchData {
fetchedResultsController = nil;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Object" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSString *sortKey = #"sortKey";
NSString *cacheName = #"myNewCache";
BOOL ascending = YES;
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortKey ascending:ascending];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:sortKey cacheName:nil];
self.fetchedResultsController = aFetchedResultsController;
fetchedResultsController.delegate = self;
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];
NSError *error;
if (![[self fetchedResultsController] performFetch:&error]) {
// Update to handle the error appropriately.
NSLog(#"Fetch failed");
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
exit(-1); // Fail
}
[self.tableView reloadData];
}
When I call this method, it works great. The table immediately re-orders itself to use the new section info, and the new sorting parameters. But if I add or remove items to the data, the TableView doesn't update its info, causing a crash. I can NSLog the count of the total number of objects in the fetchedResultsController, and see it increase (and decrease) but if I NSLog the return values for numberOfRowsInSection to monitor a change there, the method gets called, but the values don't change. The get the following crash (for addition, but the deletion one is similar)
Invalid update: invalid number of rows in section 2. The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted). with userInfo (null)
If I restart the app, I see the added item, or do not see the deleted item, so I am modifying the datasource correctly.
Any ideas?
It's possible that the old controller is still alive. If so, it might still be calling the tableview controller as its delegate and activating the table update using it's own data.
I would suggest logging the fetched results controller object in numberOfRowsInSection to confirm that it using the new controller. You should set the old controller's delegate to nil before assigning the new one.
On one occasion, adding:
- (void)viewDidDisappear:(BOOL)animated
{
self.fetchedResultsController.delegate = nil;
}
Solved a similar issue which arises when number of rows changes but data held is still different and not up-to-date.

NSFetchedResultsController with sections created by first letter of a string

Learning Core Data on the iPhone. There seem to be few examples on Core Data populating a table view with sections. The CoreDataBooks example uses sections, but they're generated from full strings within the model. I want to organize the Core Data table into sections by the first letter of a last name, a la the Address Book.
I could go in and create another attribute, i.e. a single letter, for each person in order to act as the section division, but this seems kludgy.
Here's what I'm starting with ... the trick seems to be fooling the sectionNameKeyPath:
- (NSFetchedResultsController *)fetchedResultsController {
//.........SOME STUFF DELETED
// Edit the sort key as appropriate.
NSSortDescriptor *orderDescriptor = [[NSSortDescriptor alloc] initWithKey:#"personName" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:orderDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:#"personName" cacheName:#"Root"];
//....
}
Dave DeLong's approach is good, at least in my case, as long as you omit a couple of things. Here's how it's working for me:
Add a new optional string attribute
to the entity called
"lastNameInitial" (or something to
that effect).
Make this property transient. This
means that Core Data won't bother
saving it into your data file. This
property will only exist in memory,
when you need it.
Generate the class files for this
entity.
Don't worry about a setter for this
property. Create this getter (this is
half the magic, IMHO)
// THIS ATTRIBUTE GETTER GOES IN YOUR OBJECT MODEL
- (NSString *) committeeNameInitial {
[self willAccessValueForKey:#"committeeNameInitial"];
NSString * initial = [[self committeeName] substringToIndex:1];
[self didAccessValueForKey:#"committeeNameInitial"];
return initial;
}
// THIS GOES IN YOUR fetchedResultsController: METHOD
// Edit the sort key as appropriate.
NSSortDescriptor *nameInitialSortOrder = [[NSSortDescriptor alloc]
initWithKey:#"committeeName" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:nameInitialSortOrder]];
NSFetchedResultsController *aFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:#"committeeNameInitial" cacheName:#"Root"];
PREVIOUSLY: Following Dave's initial steps to the letter generated issues where it dies upon setPropertiesToFetch with an invalid argument exception. I've logged the code and the debugging information below:
NSDictionary * entityProperties = [entity propertiesByName];
NSPropertyDescription * nameInitialProperty = [entityProperties objectForKey:#"committeeNameInitial"];
NSArray * tempPropertyArray = [NSArray arrayWithObject:nameInitialProperty];
// NSARRAY * tempPropertyArray RETURNS:
// <CFArray 0xf54090 [0x30307a00]>{type = immutable, count = 1, values = (
// 0 : (<NSAttributeDescription: 0xf2df80>),
// name committeeNameInitial, isOptional 1, isTransient 1,
// entity CommitteeObj, renamingIdentifier committeeNameInitial,
// validation predicates (), warnings (), versionHashModifier (null),
// attributeType 700 , attributeValueClassName NSString, defaultValue (null)
// )}
// NSInvalidArgumentException AT THIS LINE vvvv
[fetchRequest setPropertiesToFetch:tempPropertyArray];
// *** Terminating app due to uncaught exception 'NSInvalidArgumentException',
// reason: 'Invalid property (<NSAttributeDescription: 0xf2dfb0>),
// name committeeNameInitial, isOptional 1, isTransient 1, entity CommitteeObj,
// renamingIdentifier committeeNameInitial,
// validation predicates (), warnings (),
// versionHashModifier (null),
// attributeType 700 , attributeValueClassName NSString,
// defaultValue (null) passed to setPropertiesToFetch: (property is transient)'
[fetchRequest setReturnsDistinctResults:YES];
NSSortDescriptor * nameInitialSortOrder = [[[NSSortDescriptor alloc]
initWithKey:#"committeeNameInitial" ascending:YES] autorelease];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:nameInitialSortOrder]];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:#"committeeNameInitial" cacheName:#"Root"];
I think I've got yet another option, this one uses a category on NSString...
#implementation NSString (FetchedGroupByString)
- (NSString *)stringGroupByFirstInitial {
if (!self.length || self.length == 1)
return self;
return [self substringToIndex:1];
}
#end
Now a little bit later on, while constructing your FRC:
- (NSFetchedResultsController *)newFRC {
NSFetchedResultsController *frc = [[NSFetchedResultsController alloc] initWithFetchRequest:awesomeRequest
managedObjectContext:coolManagedObjectContext
sectionNameKeyPath:#"lastName.stringGroupByFirstInitial"
cacheName:#"CoolCat"];
return frc;
}
This is now my favorite approach. Much cleaner/easier to implement. Moreover, you don't have to make any changes to your object model class to support it. This means that it'll work on any object model, provided the section name points to a property based on NSString
Here's how you might get it to work:
Add a new optional string attribute to the entity called "lastNameInitial" (or something to that effect).
Make this property transient. This means that Core Data won't bother saving it into your data file. This property will only exist in memory, when you need it.
Generate the class files for this entity.
Don't worry about a setter for this property. Create this getter (this is half the magic, IMHO)
- (NSString *) lastNameInitial {
[self willAccessValueForKey:#"lastNameInitial"];
NSString * initial = [[self lastName] substringToIndex:1];
[self didAccessValueForKey:#"lastNameInitial"];
return initial;
}
In your fetch request, request ONLY this PropertyDescription, like so (this is another quarter of the magic):
NSDictionary * entityProperties = [myEntityDescription propertiesByName];
NSPropertyDescription * lastNameInitialProperty = [entityProperties objectForKey:#"lastNameInitial"];
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObject:lastNameInitialProperty]];
Make sure your fetch request ONLY returns distinct results (this is the last quarter of the magic):
[fetchRequest setReturnsDistinctResults:YES];
Order your results by this letter:
NSSortDescriptor * lastNameInitialSortOrder = [[[NSSortDescriptor alloc] initWithKey:#"lastNameInitial" ascending:YES] autorelease];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:lastNameInitialSortOrder]];
execute the request, and see what it gives you.
If I understand how this works, then I'm guessing it will return an array of NSManagedObjects, each of which only has the lastNameInitial property loaded into memory, and who are a set of distinct last name initials.
Good luck, and report back on how this works. I just made this up off the top of my head and want to know if this works. =)
I like Greg Combs answer above. I've made a slight modification so that strings like "Smith" and "smith" can appear in the same section by converting the strings to upper case:
- (NSString *)stringGroupByFirstInitial {
NSString *temp = [self uppercaseString];
if (!temp.length || temp.length == 1)
return self;
return [temp substringToIndex:1];
}
I encounter this issue all the time. The solution that seems best that i always come back to is to just give the entity a real first initial property. Being a real field provides for more efficient searching and ordering as you can set the field to indexed. It doesn't seem like it's too much work to pull the first initial out and populate a second field with it when the data is first imported / created. You have to write that initial parsing code either way, but you could do it once per entity and never again. The drawbacks seem to be you are storing one extra character per entity (and the indexing) really, that's likely insignificant.
One extra note. I shy away from modifying the generated entity code. Maybe i'm missing something, but the tools for generating CoreData entities do not respect any code i might have put in there. Either option i pick when generating the code removes any customizations i might have made. If i fill up my entities with clever little functions, then i need to add a bunch of properties to that entity, i can't regenerate it easily.
swift 3
first, create extension to NSString (because CoreData is using basically NSString)
extension NSString{
func firstChar() -> String{
if self.length == 0{
return ""
}
return self.substring(to: 1)
}
}
Then sort using firstChar keypath, in my case, lastname.firstChar
request.sortDescriptors = [
NSSortDescriptor(key: "lastname.firstChar", ascending: true),
NSSortDescriptor(key: "lastname", ascending: true),
NSSortDescriptor(key: "firstname", ascending: true)
]
And Finally
Use the firstChar keypath for sectionNameKeyPath
let controller = NSFetchedResultsController(fetchRequest: request, managedObjectContext: context, sectionNameKeyPath: "lastname.firstChar", cacheName: "your_cache_name")
I think I have a better way to do this. Instead of using transient property, in view will appear. Recalculate the derived property of the NSManagedObject and save the context.After the changes you can just reload the table view.
Here is an example of calculating the number of edges of each vertex, then sort the vertexes by the number of the edges. In this example, Capsid is vertex, touch is edge.
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView endUpdates];
[self.tableView reloadData];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:#"Capsid"];
NSError *error = nil;
NSArray *results = [self.managedObjectContext executeFetchRequest:request error:&error];
if (error) {
NSLog(#"refresh error");
abort();
}
for (Capsid *capsid in results) {
unsigned long long sum = 0;
for (Touch *touch in capsid.vs) {
sum += touch.count.unsignedLongLongValue;
}
for (Touch *touch in capsid.us) {
sum += touch.count.unsignedLongLongValue;
}
capsid.sum = [NSNumber numberWithUnsignedLongLong:sum];
}
if (![self.managedObjectContext save:&error]) {
NSLog(#"save error");
abort();
}
}
- (NSFetchedResultsController *)fetchedResultsController
{
if (__fetchedResultsController != nil) {
return __fetchedResultsController;
}
// Set up the fetched results controller.
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Capsid" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
// NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"timeStamp" ascending:NO];
// NSSortDescriptor *sumSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:#"sum" ascending:NO];
// NSArray *sortDescriptors = [NSArray arrayWithObjects:sumSortDescriptor, nil];
[fetchRequest setReturnsDistinctResults:YES];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:#"sum" ascending:NO];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
*/
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
return __fetchedResultsController;
}