0
votes

Scenario:

I have an expense tracking iOS Application and I am storing expenses from a expense detail view controller into a table view (with fetched results controller) that shows the list of expenses along with the category and amount and date. I do have a date attribute in my entity "Money" which is a parent entity for either an expense or an income.

Question:

What I want is to basically categorize my expenses for a given week, a month, or year and display it as the section header title for example : (Oct 1- Oct 7, 2012) and it shows expenses amount and related stuff according to that particular week. Two buttons are provided in that view, if I would press the right button, it will increment the week by a week (Oct 1- Oct 7, 2012 now shows Oct8 - Oct 15, 2012) and similarly the left button would decrement the week by a week.

My question is how would I achieve the above mentioned. I have some pseudo code written - Any help would be appreciated.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(date == %@)", dateToFilterFor];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Expense" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setPredicate:predicate];

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"SomeCacheName"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

What should be the predicate in order to filter it by a week? Also, I am thinking to have a section name in fetched result controller, but what would it be?

1
NSPredicate *tomorrowsData = [NSPredicate predicateWithFormat:@"(date >= %@) AND (date <= %@)", day1, day2]; - iDev
@ACB thanks but that doesn't really answer my question. I need to know how do we calculate the week and put it in the predicate. You gave me a rough idea of a predicate. - Angad Manchanda
You can use NSDate and associated methods for that right? Add/Remove 7 days from both days when button is pressed. Is it there you are facing issues? - iDev
@acb I am not able to figure out how shall I accomplish that, like displaying a week (starting day - sunday in NSCalendar) and then how to increment the week by another week on press of a button and then displaying that in a predicate. Can you please answer all of that inside the code I wrote above. - Angad Manchanda
@ACB In your answer, what is day1, day2? I mean that should be a week, like start date of the week and end date of the week, right? - Angad Manchanda

1 Answers

0
votes

Assuming that user has selected Oct 1 and Oct 7,

NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"MM/dd/yyyy"];

NSString *str1 = @"10/01/2012";
NSDate *date1 = [formatter dateFromString:str1];

NSString *str2 = @"10/07/2012";
NSDate *date2 = [formatter dateFromString:str2];

Use it as,

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(date >= %@) AND (date <= %@)", date1, date2];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Expense" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setPredicate:predicate];

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"SomeCacheName"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

In order to add the dates,

int addDaysCount = 7; //try with negative values for fetching previous week.

NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setDay:addDaysCount];

NSDate *newDate1 = [[NSCalendar currentCalendar] 
                              dateByAddingComponents:dateComponents 
                                              toDate:date1 options:0];
NSDate *newDate2 = [[NSCalendar currentCalendar] 
                              dateByAddingComponents:dateComponents 
                                              toDate:date2 options:0];

Use the above predicate with newDate1 and newDate2 to fetch new expense corresponding to these dates.

If you want to get the sunday of a particular week, use NSDateComponents.

NSDateComponents *components = [NSDateComponents new]; 
components.weekday = 1;//represents sunday 
components.year = 2012; 
components.weekOfMonth = 1;
components.month = 10; //etc..

NSDate *date1 = [[NSCalendar currentCalendar] dateFromComponents: components]; 

For more details on NSDateComponents and its properties check