• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Core Data entity fetching by date
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Core Data entity fetching by date


  • Subject: Core Data entity fetching by date
  • From: Michael Swan <email@hidden>
  • Date: Sat, 08 Nov 2008 21:25:47 -0500

So I am looking for a better way to fetch all entities from each month of the year so that I can get a total for each month.

Each entity has a date, amount, & type (which is a to one relationship to a type entity, the reverse is to-many). In the end I want to break things down so that the app can tell you "Jan type A = $300, type B = $100, ..."
I have a method that currently tells me how many entities are from each month of the year and can easily then add the amounts. The problem is that I have 8 pages of code so far and I haven't actually added up the totals or split them be type. I am looking to see if anyone has any ideas to make this be less code and therefore more efficient. Below are the tedious steps I have to go through:


Get the correct NSEntityDescription
Create the basic strings for beginning and end of the month
NSString *greaterPredString = [NSString stringWithFormat:@"%@ >= % %@", @"date"];
NSString *lessPredString = [NSString stringWithFormat:@"%@ < %%@", @"date"];
Create an NSMutableArray and put 12 NSDateComponents into it.
Get the current date.
Create an NSCalendar (NSGregorianCalendar)
Extract the year form the current date.
Enumerate through the NSDateComponents array setting each NSDateComponents to the current year, day 1, month i, hour 0. minute 0, second 0.
Create one last NSDateComponents for the first day of the next year.
Create an NSDate from each of the NSDateComponents and put them in an NSMutableArray.
Create two predicates for each month:
NSPredicate *janStartPred = [NSPredicate predicateWithFormat:greaterPredString, jan];
NSPredicate *janEndPred = [NSPredicate predicateWithFormat:lessPredString, feb];
Create NSCompoundPredicate () for each month with the start and end predicates:
NSPredicate *janPred = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects: janStartPred, janEndPred, nil]];
Create an NSFetchRequest for each month. Setting the entity and predicate.
Create an NSError to go with each NSFetchRequest.
Create an NSArray to go with each NSFetchRequest and fill it with the fetch results: NSArray *janArray = [[self managedObjectContext] executeFetchRequest:janReq error:&janError];
Check to see if there were any results from the fetch.
Enumerate through the != nil arrays, retrieve the amount attribute of each entity and add them together.
This of course does not separate entities for a given month by type to be summed that way.



I am still relatively inexperienced and am certain that I am just missing the easy way to do this. Any ideas are appreciated.


Thank  You,
Mike Swan
http://www.michaelsswan.com


"As human beings our greatness lies not so much in being able to remake the world... as being able to remake ourselves" - Gandhi
_______________________________________________


Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Core Data entity fetching by date
      • From: Quincey Morris <email@hidden>
  • Prev by Date: Xcode's New Data Model reads ivars as attributes ?!?
  • Next by Date: RE: Process Priority
  • Previous by thread: Re: Xcode's New Data Model reads ivars as attributes ?!?
  • Next by thread: Re: Core Data entity fetching by date
  • Index(es):
    • Date
    • Thread