Fetch Unique Calendar Dates For Core Data Object - core-data

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.

Related

Access - Calculated field (running average)

I am trying to generate an Access database with information which is currently in endless sheets and tables in Excel.
I would like to know if there is any way to add a field to one table which is a calculation (average value) based on several other cells.
I need to calculate the running 6 months average value of another field which contains 1 value per month.
Hopefully the previous image shows what I mean.
What is the best approach to import this functionality into access?
You wouldn't normally store a calculated field in Access, you would run a query that provides you the calculation on the fly.
Without seeing your data structure it is impossible to tell you how to calculate the answer you need, but you would need your data correctly normalised in order to make this simple.

Excel: Averaging the values from a set of rows, but only include them if they have a specific value in a column

I'm looking for help dynamically averaging the column values of every item in an Excel table that has a given value in one of its columns. Specifically:
I have an Excel sheet where each row represents an entity in a video game I am working on, and each column is a numerical value for different attributes on these entities. Movement Speed, Health, Attack Damage, etc. Each of these rows also has a column where I tag the row with the name of the class that this entity is a part of: "tank", "support", etc. This table has roughly a hundred items in it, and is likely to grow to two or three times that size.
It looks something like this:
What I would really like to do is have, on a separate tab, a table where each row represents one of the classes, and shows the average value of all of the entities that have that class in their "group" column. And I want it to automatically include new entities of that class as they are added to the first table.
It would look something like this, where these values are automatically generated from the data in the first table (I have no problem manually entering the class names, I just need the numerical data to be driven):
I imagine that the solution will be a complex, nested pile of VLOOKUPs and MATCHes and other Excel functions, but I am not really sure how to accomplish this. I didn't even know the proper terminology to search for existing answers to this question, so I hope that it isn't too redundant. Thanks very much for any advice you have!
Version: I am using Excel 2013.
I think all you need is a pivot table. (its been around since the 90s?) - and very useful!
there are lots of ways of refreshing etc depending on where the data comes from
http://office.microsoft.com/en-us/excel-help/pivottable-reports-101-HA001034632.aspx

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];

How do I create report-like data tables in Excel?

In the past I have created websites that extract data from a database and format it using tables.
Now, I am trying to do the same thing but with Excel, and I'm lost. I am used to using SQL commands to extract data from given fields and then sort/manipulate it.
Currently, I am able to print a report that provides me with an Excel spreadsheet full of raw data, but I would like to make my life easier and organize it into a report.
The column that I would like to reference contains duplicates, but the data in the adjacent columns is different.
To give an example, assume I had a spreadsheet of sales transactions. One column would be the Customer ID, and the adjacent columns would contain the quantity, the cost per unit, total cost, order ID, etc.
What I would want to do in this case would be to select all the transactions with the same Customer ID and add them together based on their Order ID. Then, I would want to print the result to a second sheet.
I realize that I can use built-in functions to accomplish this, but I would also like to format this report evenually using VBA. Also, since I will have a variable number of rows that differ from one report to the next, I haven't encountered a fucnction that will allow you to add rows.
I'm assuming this must be done with VBA.
Well you can do it manually, but it would take ages. So VBA would be good, particularly as you would be able to generate future reports quickly.
My interpretation of what your saying is that each row in your report will be the total for one customer ID. If it's something else, I imagine the below will still be mostly relevant.
I think it would be a bit much to give you the full answer, particularly as you haven't provided full detail but to take a stab at what you'd do:
Create your empty report page, whether it be a new worksheet or a new workbook
Loop through the table (probably using While next is not empty)
a. Identifying if a row is for a customer ID you haven't covered yet
i. If so then add a new entry in your report
ii. Else add it to the existing customer ID record (loop through until you find it)
Format your report so it looks pretty, e.g:
a. Fill the background in white
b. Throw in some filled bars
c. Put in good titles and totals etc.
For part 1, it might be better building an array first and then dumping the contents into the report. It depends how process intensive it will be - if very intense, an array should shave off time.

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