small medium large xlarge

Back to: All Forums  Core Data
Picture_3_09-23-14_pragsmall
19 Feb 2010, 03:01
Jeremy W. (20 posts)

I’m trying to build a query to select the contacts that have a birthdate coming up in the next 30 days. the DOB are stored as a NSDate but I only really care about month and day not the year because the year is in the past. So how to I tell the predicate that I want to compare the Month and Day of the DateAttribute. so I can Pass in the current month and day and then pass in the month and day 30 days from now.

I’ve searched around and I dont see an example of how to tell it I want to compare to the day or month of the NSDate object stored in CoreDate.

Anyone have a clue?

Thanks for any help!

Avatarsmall_pragsmall
19 Feb 2010, 04:10
Marcus S. Zarra (284 posts)

There is not a clear way to do that. Your best way is to construct, dynamically, two dates, one for today and one for thirty days from now and plug those into a predicate.

Picture_3_09-23-14_pragsmall
22 Feb 2010, 16:37
Jeremy W. (20 posts)

that wont work because the DOB will likely be in the past years, not the future. So two dates, today and 30 days from today will never return records that have a DOB that will be in the next 30 days because while the Month and day might be in the next 30 days, when you compare dates it compares the whole date, including the year. Thus my delima, I cant believe no one has tried to do this before or that core data doesn’t support this, every data engine I have worked with before supports this in some way.

Avatarsmall_pragsmall
22 Feb 2010, 18:06
Marcus S. Zarra (284 posts)

Ahh, I misunderstood. Then in that case I would create two derived attributes in the data store that contain the month and day. You can set these up to be populated whenever the date attribute is updated. You can then index and search these columns quickly and easily.

Picture_3_09-23-14_pragsmall
22 Feb 2010, 19:25
Jeremy W. (20 posts)

yea that is what I came up with too. When the code updates the date, it splits the month and date out and save to a NSNumber field on the record. The to query the data I use this code, I’m posting so others can learn from the code.

NSTimeInterval secondsPerDay = 24 * 60 * 60 * 30;

NSDate *nextThrityDays = [[NSDate alloc]
			  initWithTimeIntervalSinceNow:secondsPerDay];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM"];
NSNumber *StartMonth = [NSNumber numberWithInt:[[dateFormatter stringFromDate:[NSDate date]] intValue]];
NSNumber *EndMonth = [NSNumber numberWithInt:[[dateFormatter stringFromDate:nextThrityDays] intValue]];
[dateFormatter setDateFormat:@"dd"];
NSNumber *StartDay = [NSNumber numberWithInt:[[dateFormatter stringFromDate:[NSDate date]] intValue]];
NSNumber *EndDay = [NSNumber numberWithInt:[[dateFormatter stringFromDate:nextThrityDays] intValue]];
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"( sysDOBMonth = %@ && sysDOBDay >= %@ ) || ( sysDOBMonth = %@ && sysDOBDay <= %@ ) ", StartMonth, StartDay, EndMonth, EndDay];

I think this is the worst Database and App design ever, I did alot of research on this and some apple documentation even stated that I need to De normalize my database, and I have had to in other areas to be able to query the data. Apple has a long way to go with there design suite if the want people to convert their apps from systems that support my type of request. Makes you question how apple makes so much money with such bad design.

Avatarsmall_pragsmall
22 Feb 2010, 20:54
Marcus S. Zarra (284 posts)

One thing to keep in mind about Core Data. It is first and foremost it is an Object Hierarchy that happens to persist to a database. It is not and probably never will be a database engine.

Picture_3_09-23-14_pragsmall
23 Feb 2010, 20:25
Jeremy W. (20 posts)

I understand that, But there is plenty of room for improvement. A great tool to look at is Developer Express’s eXperss Persistent Objects (XPO), its basically core data for .net. Much more mature, to bad the company does not develop frameworks for Objective C.

You must be logged in to comment