Friday, July 10, 2009

Core Data Default Dates in Data Model

I'm leaving this blog entry, but do not use this tip - there is a flaw in it

When creating a managed object model using Xcode's Data Model Editor, I knew that there was a way to specify a default value for a date attribute that would cause it to default to the current date and time. It's one of those things I need to do about twice a year and can never, ever remember how it's done. Apple's documentation didn't yield an answer (it might be in there, but it's not easy to find). Yes, you can create a custom subclass of NSManagedObject and set the value in awakeFromInsert:, but for those situations where you don't want to subclass, or don't feel like it, I knew there was a way and it was frustrating me that I couldn't remember it or find it.

Thanks to Twitter, I got the answer, and it's a cooler answer than I remembered. The Default field for date attributes will interpret natural language strings put in quotes. It appears to be that anything that NSDate's natural language parser can handle, the date attribute's default field can handle. "Tomorrow", "Today", "Now", "Yesteday at Breakfast".

WHOAH: After a user comment alerted me to this, I tested it out and sure enough, the natural language string entered into the default field is actually determined at compile time, not at runtime, so 'today' will always result in the same date. Do not use this technique!

via Shane Crawford



7 comments:

Paul Warren said...

Jeff, I seem to remember being pleased to find this when i first starting using Core Data BUT after finding that "today" worked perfectly the first day I was disappointed to find that it was the same date the next day and the next. It seems that the editor interpreted the natural language string and not core data at runtime as I'd perhaps naively expected.

Jeff LaMarche said...

You are right, I just confirmed it. Thanks for the heads up!

mhoy said...

This strikes me as rather funny.

Jeff LaMarche said...

mhoy:

It's a riot, actually. It goes to show you that if something isn't documented, it's potentially unsafe to use.

Jeff

Jeremy said...

If you go in to the data model and on the field specify today or now, it does work at run time, I just tested it in my app running 3.2.1

I got the info from this site
http://shanecrawford.org/2008/57/coredatas-default-date-value/

Edwin said...

scrub m65 kamagra attorney lawyer body scrub field jacket lovegra marijuana attorney injury lawyer

h4ns said...

I was very encouraged to find this site. I wanted to thank you for this special read. I definitely savored every little bit of it and I have you bookmarked to check out new stuff you post.

AC Milan vs Lazio Live Streaming
West Bromwich Albion vs Wigan Athletic Live Streaming
Manchester United vs Aston Villa Live Streaming
Sunderland vs Chelsea Live Streaming
Arsenal vs Everton Live Streaming
Augsburg vs Bochum Live Streaming
Racing Santander vs Valencia Live Streaming
Frosinone vs Atalanta Live Streaming
AC Milan vs Lazio Live Streaming
West Bromwich Albion vs Wigan Athletic Live Streaming
Manchester United vs Aston Villa Live Streaming
Sunderland vs Chelsea Live Streaming
Arsenal vs Everton Live Streaming
Augsburg vs Bochum Live Streaming
Racing Santander vs Valencia Live Streaming
Frosinone vs Atalanta Live Streaming
Technology News | Hot News Today | Live Stream