János
János

Reputation: 35114

How to store NSDate in Core Data?

When generating Core Data subclasses I check to use primitive data type in Core Data storage.

enter image description here

Which NSTimeInterval representation should I convert from NSDate? Or it does not matter, just use appropriately when reading / writing the same managed object attribute?

I prefere to use like: mo.creationDate = date.timeIntervalSinceReferenceDate

Upvotes: 2

Views: 1355

Answers (3)

Sathya
Sathya

Reputation: 2237

iOS's reference date counts from : 1 January 2001, GMT.
Unix Time Stamp reference date counts from : Jan 01 1970, UTC

If this is a completely offline app you can use the timeIntervalSinceReferenceDate and store the value directly.

But, if you are going to sync up with a server then generally, it is preferable to send the data to server as a unix time stamp. Because a desktop uses unix time Stamp as default.

So, in the second case you can either choose to save the date directly as a UnixTimeStamp and convert it for local use using a getter in Model Class (refer : @VladZ) or You can save it as iOS's reference date and convert it to UnixTimeStamp each time you send it to server.

Upvotes: 2

Vladyslav Zubkov
Vladyslav Zubkov

Reputation: 365

Use - timeIntervalSince1970 and save this value in CoreDate

mo.creationDate = [NSNumber numberWithDouble:timeInterval];

Upvotes: 1

Pablo A.
Pablo A.

Reputation: 2052

You're adding a time interval, that it's not an object of type NSDate.

Try to add a NSDate with the desired time interval:

Check this out

Upvotes: 0

Related Questions