PDA

View Full Version : Core Data Date formatting issue




beaglboy
Jun 21, 2009, 07:56 AM
Hopefully someone can help me with this one. I have a contact management app that has date fields for "Last Contact date" and "Next Contact Date". I created a search predicate that would search on next contact date and display only the records for that date. It worked great until I decided I needed to create a user preference so the next contact date would automatically be some date in the futrure. I created a subclass of NSManaged object and put my code there.

Everything works except the date it writes to the database looks like this: >name="nextcontact" type="date">268045271.25241801142692565918<

For some reason my search predicate does not find the records with this format. Before I changed the date with my subclass the date field in the database looked like this: >266515200.00000000000000000000< (always containing the 00.00000000000000000000 string)
With this format the search predicate works fine.

My code to add to the date field looks like this:
- (void)awakeFromInsert {
int t;
t = [[NSUserDefaults stardardUserDefaults] integerForKey:@"nextContactDate"]; // gets # of days to add to date from user preferences
[self setValue:[NSDate dateWithTimeIntervalSinceNow: (3600*24*t)] forKey:@"nextContact"]; // sets the value of nextContact in a new record by adding 't'
}

The search predicate is quite simple and looks like this: nextContact = CAST($value "NSCalendarDate")

My question is how can I fomulate a date that will save as the second example above (or possibly change the predicate so it will work with the new date format).

Thanks!