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];
Related
here's my input and my desidered output. Problem Is: I have a columns for product, price, and date and I want to build a cross table where I can see the price of the day before for every product.
How can I calculate column d-1 (day - 1) in my data table? Should I use Intersect/Over functions? What If I want to calculate my d-1 column on-the-fly in a cross table?
You have some options here, depending on how you want to display everything.
If all you want is an overall snapshot of how it changed between Today and Yesterday, probably the simplest thing to do is the following:
Avg(If([DATE]=Date(DateTimeNow()),[Price],NULL)) as [Today's Price],
Avg(If([DATE]=Date(DateAdd("day",-1,DateTimeNow())),[Price],NULL)) as [Yesterday's Price],
Avg(If([DATE]=Date(DateTimeNow()),[Price],NULL)) - Avg(If([DATE]=Date(DateAdd("day",-1,DateTimeNow())),[Price],NULL)) as [d-1]
This can be very useful for a dashboard or summary, and if you replace the "DateTimeNow" component with a Document Property controlled by an Input field, this can quickly show your user the change on a specific day.
The Over functions could still be quite useful, especially if you're using a date hierarchy of some form.
It will update on the fly to accommodate the columns grouped across the top, so if you're displaying by date, you can see how it's been changing each day, and if you're displaying by month, you can see it that way instead.
Avg([Price]) as [Avg Price],
Avg([Price]) - Avg([Price]) OVER (PreviousPeriod([Axis.Columns])) as [Avg Price Change]
I have a database in Tableau from an Excel file. Every row in the database is one ticket (assigned to an Id of a customer) for a different theme park across two years.
The structure is like the following:
Every Id can buy tickets for different parks (or same park several times), also in different years.
What I am not able to do is flagging those customers who have been in the same park in two different years (in the example, customer 004 has been to the park a in 2016 and 2017).
How do I create this calculated field in Tableau?
(I managed to solve this in Excel with a sumproduct fucntion, but the database has more than 500k rows and after a while it crashes / plus I want to use a calculated field in case I update the excel file with a new park or a new year)
Ideally, the structure of the output I thought should be like the following (but I am open to different views, as long I get to the result): flag with 1 those customers who have visited the same park in two different years.
Create a calculated field called customer_park_years =
{ fixed [Customerid], [Park] : countd([year]) }
You can use that on the filter shelf to only include data for customer_park_years >= 2
Then you will be able to visualize only the data related to those customers visiting specific parks that they visited in multiple years. If you also want to then look at their behavior at other parks, you'll have to adjust your approach instead of just simply filtering out the other data. Changes depend on the details of your question.
But to answer your specific question, this should be an easy way to go.
Note that countd() can be slow for very large data sets, but it makes answering questions without reshaping your data easy, so its often a good tradeoff.
Try this !
IFNULL(str({fixed [Customerid],[Park]:IF sum(1)>1 then 1 ELSE 0 END}),'0')
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.
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.
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.