How can I change section ordering with NSFetchedResultsController? - core-data

I'm setting up an NSFetchRequest with the following sort descriptor:
[sortDescriptors addObject:[NSSortDescriptor sortDescriptorWithKey:#"color" ascending:YES]];
I'm setting the sectionNameKeyPath for the NSFetchedResultsController to be the "color" attribute.
The results are sorted into sections based on color, as I would expect.
It's valid for the "color" attribute to be an empty string "" for some objects. Those are all grouped together in the first section, since they sort before all other names. However, I'd like them to appear as the final section, not the first section, without affecting the search order of the remaining objects.
For example, currently the sections might be:
""
"green"
"red"
"yellow"
But I'd like the sections to be in the order:
"green"
"red"
"yellow"
""
Is there some way to write the sort descriptor to get this behavior? I've been unable to come up with anything myself.

A sort descriptor for a (SQLite based) Core Data fetch request can only use some standard comparator methods and only use (persistent) attributes stored in the database. To get
a non-standard order you will have to add an additional attribute to the entity and
use that for sorting.

Related

Map through JSON object in DynamoDB

I have stored my column as a JSON object in my DynamoDB table.
receiver:[{id: 'r1', name: 'a'},{id: 'r2', name: 'b'},{id: 'r3', name: 'c'},]
I want to get the item if r1 is a receiver.
I tried using the FilterExpression receiver[0].id = 'r1' and it did return the item. However, what if the index of r1 was not at 0? How would i be able to map through the object?
DynamoDB is not great for search, especially within complex attribute types like lists or maps. While useful, these complex attribute types are not well suited for search.
One place where they can be useful is if you have known keys. For example, imaging an attribute named phone_numbers that stores home, work and cell phone numbers:
phone_numbers:{
home: "(555) 123-4567"},
work: "(555) 246-8910"},
cell: "(123) 345-1231"}
}
Storing your data in this way allows you to fetch a users home/work/cell phone number. However, it does not support searching for phone numbers by area code.
If you need to search the data stored in your complex attribute, you'll need to get the data in a more useful format (e.g. in it's own attribute). Better yet, incorporate it into your primary key!

how to model colors in graph database [neo4j[

I am practicing with Neo4j. I am trying to model colors and their relationships but I don't know what to do with different names of colors.
For example, it looks like (from what I read on the internet), aqua, cyan, teal are more or less the same color.
I want to model colors such as I can classify each color in it's category (primary, secondary, tertiary), in its family (warm, cool, neutral).
I also want to represent shades, tints and tones of different colors.
How do I model that x is a tint of y and y is a primary color for example? It is becoming difficult.
I want each color to be a SEPARATE NODE.
thank you for your time.
Honestly this really depends on how you want to query your data. You could represent color as simply as a hex code or a serialized JSON string of the properties that you describe above and then you would just need a property. But if colors are a fundamental part of your model then you could get really detailed and split things out into individual nodes and relationships. I'll assume that here.
So you could have, for example, Color, Level, Family, Shade, etc... labels. For the labels after Color you could just create all of them upfront, or you could use MERGE to create them only when necessary.
Then you could simply create color nodes and relationships like:
CREATE (color:Color {hex: '#FFFFFF', name: 'White'})
and then:
MATCH (white:Color {hex: '#FFFFFF'}), (primary:Level {name: 'Primary'})
CREATE (white)-[:IS_LEVEL]->(primary)
Again, this would be if you want to query by finding common paths between colors. If you just want to be able to tell if #FFFFFF is a primary color you could have a level property with the vaule Primary.
Side note: I suggest reading Randall Munroe's color survey results blog post because it is awesome.
EDIT:
Based in your comment, if you want to have different names for a color you could either have an array property for the name (or perhaps a name property which is a string and an alternative_names property which is an array), or you could have a ColorName node with a name property and then a (:Color)-[:HAS_NAME]->(:ColorName) relationship.
One consideration if your database were to grow very large is that Neo4j doesn't yet support indexing on array properties (I think you could do it with legacy indexes, though). So something like this would work:
MATCH (color:Color) WHERE 'Red' IN color.names RETURN *
But it will need to look through ever Color node. If you had a ColorName label then you could make an index for ColorName(name). But it does come with the tradeoff that the more you extract properties into separate nodes, your queries start becoming more difficult to maintain because you need to include those extra nodes every time you want to display information about a color. You start to get into queries like;
MATCH (color:Color)
WHERE color.hex = '#FFFFFF'
OPTIONAL MATCH (color)-[:HAS_LEVEL]->(level:ColorLevel)
WITH color, level
OPTIONAL MATCH (color)-[:HAS_NAME]->(color_name:ColorName)
WITH color, level, collect(color_name.name) AS names
// etc....
RETURN color, level, names, etc...

Comparison function for Core Data NSFetchedResultsController

I am using NSFetchedResultsController to section my data into separate sections. The main sortDescriptor I give is this:
NSSortDescriptor *sortDescriptorSectionLetter = [[NSSortDescriptor alloc] initWithKey:#"sectionLetter" ascending:YES selector:#selector(localizedCompare:)];
Now my sectionLetter has a default value of "#". When the tableView is sectioned and presented, it places the '#' section at the top of the table. I want this to show up at the bottom of the list (like the Contacts app does it). But I can't figure out how to accomplish this.
I've also tried different types of characters as the default instead of #, like � and ~, but this doesn't seem to work either. According to the Core Data Programming Guide, I can't pass in a customized comparison function here.
So I'm not sure what my options are here (other than doing the sectioning myself and losing all the FRC delegate goodness to reload my tableviews with animation). Any good ideas?
Notice the remark in the documentation for NSFetchedResultsController:
Subclassing Notes
You create a subclass of this class if you want to customize the creation of sections and index titles. You override sectionIndexTitleForSectionName: if you want the section index title to be something other than the capitalized first letter of the section name. You override sectionIndexTitles if you want the index titles to be something other than the array created by calling sectionIndexTitleForSectionName: on all the known sections.

iphone SDK: Core Data, how to sort sections in an NSFetchedResultsController?

I have a core data app that is using the sectionNameKeyPath "group.name" with the fetchRequest in order to group the results by the group entity's name attribute. I'm grouping by group.name but I'd like to sort the sections by something other than group.name. According to the NSFetchedResultsController docs:
If the controller generates sections,
the first sort descriptor in the array
is used to group the objects into
sections; its key must either be the
same as sectionNameKeyPath or the
relative ordering using its key must
match that using sectionNameKeyPath.
Which means that the sections must be sorted in the same order the are grouped in. Despite the documentation, prior to iOS 4.2 you could get away without specifying the sectionNameKeyPath as the first sort descriptor which allowed you to sort the sections, but no longer.
What is the best way to sort sections in an NSFetechedResultsController? For example, I want my sections to be grouped by "group.name" but sorted by "group.timestamp".
From the NSFetchedResultsController docs:
You create a subclass of this class if
you want to customize the creation of
sections and index titles. You
override
sectionIndexTitleForSectionName: if
you want the section index title to be
something other than the capitalized
first letter of the section name. You
override sectionIndexTitles if you
want the index titles to be something
other than the array created by
calling
sectionIndexTitleForSectionName: on
all the known sections.

NSArrayController that is sorted and unique (no duplicates) for use in a pop-up in a core-data app

I have core data app with an entity OBSERVATION that has as one of its attributes DEALNAME.
I want to reference through Interface Builder or by making custom modifications to an NSArrayController a list of unique sorted dealnames so that I can use them in a pop-up.
I have attempted to use #distinctUnionOfSets (and #distinctUnionOfArrays) but am unable to locate the proper key sequence.
I can sort the ArrayController by providing a sort descriptor, but do not know how to eliminate duplicates.
Are the #distinct... keys the right methodology? It would seem to provide the easiest way to optimize the use of IB.
Is there a predicate form for removing duplicates?
Or do I need to use my custom controller to extract an NSSet of the specific dealnames, put them back in an array and sort it and reference the custom array from IB?
Any help would be appreciated. I am astounded that other have not tried to create a sorted-unique pop-up in tableviews.
You need to take a look at -[NSFetchRequest returnsDistinctResults]. That is the level you need to be handling the uniquing of data.
Although I do not have a definitive answer for you, I think there are two ways you can go about it.
The way you already started. You need to bind the contents array of the PopUp button, not just against the arrayController.arrangedObjects, but continue on the path and somehow filter only objects with distinct "DealName"s. This means - the arrayController presents ALL the entities (and may sort them for you) but the PopUp button will have its contents filter via some sophisticated binding to the array controller.
Make your filtering at the ArrayController level (as suggested in another answer here). Here it depends how you set up the array controller. If It is set up to use an "Entity" (vs. "Class") which means the array controller will fetch CoreData entities directly - you can modify its "Fetch" to only bring a subset of the "OBSERVATION" entities with distinct values of "DEALNAME". I don't know how to control WHICH entities are filtered out in this case. Otherwise, you can setup the arrayController to work with "Class" objects, and then you can fetch the entities yourself (in code) and populate the arrayController programmatically, with just the entities you like.
In the second option, the Popup button should be bound normally to the arrayController's arrangedObjects.

Resources