Arrange tableViewCell by creating Date - core-data

I have app where i use CoreData to fill my tableViewCell's. In that CoreData i have one entity with 4 strings attributes and one Date attribute.
Is possible to arrange my table cells by this Date attribute?
I have trying search some manual here and on youtube, but unsuccessful.

Add a sort descriptor to your fetch. e.g.
fetchRequest.sortDescriptors = #[[NSSortDescriptor sortDescriptorWithKey:#"<name_of_date_property" ascending:YES]];

Related

Is there away to display a title for a Tabulator table?

I have multiple Tabulator tables stacked vertically on a form, each providing a unique purpose. I would like to be able to assign a title to each Tabulator without the use of a leading div element. Manipulating column titles is straight-forward but I am unable to find anything within the Tabulator documentation which suggests a means of creating a table header to span the width of all columns. Is this possible?
You could abuse the column group feature:
http://tabulator.info/examples/4.7#column-groups
"By creating groups in the column definition array, you can create multi line headers with groups of columns."

Fetch Unique Calendar Dates For Core Data Object

How do we fetch unique Calendar Dates for Core Data Objects?
For example:
If we have three emotion objects with NSDates:
"2014-08-04 20:33:42 +0000",
"2014-08-04 20:50:33 +0000",
"2014-08-04 20:50:46 +0000",
"2014-08-06 20:35:58 +0000",
"2014-08-08 20:33:49 +0000"
I want to get an array of NSDates that looks like this:
2014-08-04
2014-08-06
2014-08-08
I have explored these threads as possible solutions but neither of them work.
CoreData get distinct values of Attribute
How to count unique dates of CoreData objects?
A possibility would be to make time and date attributes for the managed objects I am searching for but that seems like a clunky work around.
Another possibility is to strip the time component of the all the NSDates that come back then filter the array of Dates again for duplicates. Again this seems really clunky and prone to breaking.
I want to get an array of NSDates with only the calendar date and then use that information to present tableview cells. When the user taps a given tableView cell I can push a viewController that displays data related to that calendar date. As time goes on the number of NSDates could get large because there will be many objects with NSDates to search for so the solution should be efficient.
You're going to have to use an approach like the ones you describe, because that's how it needs to be done.
You're storing NSDate values, but NSDate doesn't actually represent a date-- it represents a specific moment in time, to the fraction of a second (it might have been better if it were called something like NSDateTime, but it wasn't). It's really an object wrapper around an NSTimeInterval, which is just a double. Core Data just saves this value, automatically converting to/from NSDate when necessary. As a result, you don't actually have unique calendar dates in your sample data. The time of day is different, which means each of your sample values represents a different double value in the persistent store.
If you want unique date values, you need to either add a field that encodes only the date, or to fetch all of the NSDate style attributes and filter the result. I'd probably go with the first-- store the data you actually need to look up-- but either will work.

Predicate subquery for products associated by tag

I have a datamodel in core data as such:
Store <--->> Product <<--->> Tag
I'm looking to create a query that matches products based on their associated tags. A typical use scenario would be you choose to search storeA and output storeB. By selecting storeA a list of storeA products is presented. Let's say you pick productA from the list which brings you to a second viewcontroller. That second viewcontroller lists all relating productB's (because earlier you set storeB as output). I'm following this: predicate subquery to return items by matching tags but want the added store filter of products. Is subquery the way to go? How would I include a filter by selected Store?

Breaking up a Core Data Date Attribute - Good idea?

I am learning Core Data, and I have a project up and running that stores, creates, retrieves, updates, and deletes entities just fine. I am loving using Core Data so far.
I have a simple entity that has 5 attributes. One of them is a date. I call it dateCreated. I am using a TabBar in my app and I want to be able to sort things on different screens by Year and Month, and Month details. I have a couple questions about what is good practice and what is good for performance sake.
So ideally I want my Collection View to be sectioned by Year, and each cell containing a month that takes them to that months table. Currently in one of my tabs have a UITableView that loads user created data but I haven't broken it up by Year and Month yet. Meaning they can just keep putting in dates and other info and the one table just keeps getting larger. I would love to use the Date Attribute my - dateCreated attribute to break the table into multiple tables for each month in my Collection View. It seems like all the data is in place to minipulate and display it anyway I want, but I am not sure if it is the best way to go, or even if it is the standard practice when using Core Data. I would think I could use the same table to load the data based on what year and month it is. Instead of making multiple tables of months.
My questions as long winded as they are: Should I create more Date attributes or use the dateCreated that is displayed in my TableView using a formatter to show it as "Jul, 31 2013". In other words can I break up the user created data based off of my dateCreated attribute alone.
Second. I understand how to "fetch" data from the Core Data stack and get it in the TableView using NSFetchedResutsController, but what if I want to display certain attributes from an entity in a screen that doesn't have contain a table view. Should I still use NSFetchedResultsController, or should I be using something else? The Apple Docs on Fetching Data shows how to fetch specific attributes from the entity to be displayed but it seems like so much code for something so simple that I think I have looked in the wrong section.
Okay I understand this sounds a bit confusing so I am wanting to know if I should use my dateCreated attribute to break up the data, or create more Date attributes one for year, one for month and keep the one for dateCreated and sort it that way. Also if I just want to display certain attributes data without a table on screen should I still be using NSFetchedResultsController?
Thanks for all your help if you provide any. I am having a great time learning Core Data. It is really powerful.
You might want to adjust your predicate for the fetching to something like:
NSDate *startDate = ....;
NSTimeInterval length;
[[NSCalendar currentCalendar] rangeOfUnit:NSMinuteCalendarUnit
startDate:&startDate
interval:&length
forDate:startDate];
NSDate *endDate = [startDate dateByAddingTimeInterval:length];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(eDate >= %#) AND (eDate < %#)", startDate, endDate];

saving a to-many-relationship

I'm starting to learn and practice some CoreData, with no programming experience.
After searching in Web, looking for Apple samples and reading some books, I´m still stucked in one point.
I have two entities (Expenses and Month) with reciprocal relationships called "monthsOfExpense" and "expensesOfmonth" and I'm showing in a tableView the expenses of a single month.
My problem is to insert new expenses and save them: I've a view to insert new expenses where the user can insert the name of Expense, the value and the months that expense is valid (associated with the monthsOfExpense relationship, i hope).
I'm ok saving the name and the value of Expense entity, taking the string and NSDecimal number from the textFields.
My problem is how to associate that expense to a particular or several months? How can I save a textField.text as a relationship, indicating to what month an Expense belongs?
I'm starting with a textField where the user can insert a month, assuming that an Expense is valid for one month only (for learning purposes and simplification).
My idea is to allow the user to select several months (using maybe another tableView with selectable months) and association of an Expense to different months.
I know that a relationship comes as a NSSet, but I'm not being competent to save the attributes and the relationship from a View, at the same time.
Hope was cleared and many thanks in advance for trying to help.
OK. In Core data, you define your "Expense" entity, create a relationship to "Month" entity. Now create class files for these 2 entities. Then you use it by
Expense *expense1 = [NSEntityDescription insertNewObjectForEntityName:#"Expense" ....
Month *month1 = [context executeFetchRequest:......];
expense1.month = month1;
...
[context save];
In your situation, you can predefine 12 "Month" objects, giving the month name as the text property.
Then you can lookup these month objects using NSFetchRequest. Add the relevant month objects to your expense using
[expense addMonthObject:month]
Then save your managed object context.
You message the managed object for its mutableSetValueForKey:RelationshipKey,
then you add the to-many managed objects to that set. CoreData completes the other half of the relationship data.

Resources