0
votes

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.

1

1 Answers

0
votes

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