Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Swift's Date with CoreData

I have a lot of date fields in my database model. CoreData allows to use NSDate or TimeInterval to save dates depending on "Use Scalar Type" option.

enter image description here

However both these options are bad for me since I want to use dates as Date objects. Since NSDate is not implicitly convertible to Date I have to cast/convert values to Date or to implement a lot of custom setters/getters in my NSManagedObject classes.

I have tried to use ValueTransformer but it does not work with non-@objc classes like Date.

So is there a simple way to save and get Date values to/from CoreData?

like image 769
Avt Avatar asked Sep 29 '16 19:09

Avt


People also ask

How is date stored in Core Data?

In a Core Data store, a Date attribute is a double value that represents a number of seconds since 1970. Using a variety of calendars, time zones, and locales, an app can convert a Date value to different date strings, or convert a date string to different Date values.

Do I need Core Data?

The next time you need to store data, you should have a better idea of your options. Core Data is unnecessary for random pieces of unrelated data, but it's a perfect fit for a large, relational data set. The defaults system is ideal for small, random pieces of unrelated data, such as settings or the user's preferences.

What is Core Data in swift iOS?

CoreData is the framework provided by Apple to save, track, filter, and modify the data within the iOS applications. It is not the database, but it uses SQLite as it's persistent store.

How do you declare a date variable in swift?

How do you create a date object from a date in swift xcode. eg in javascript you would do: var day = new Date('2014-05-20');


2 Answers

There is a workaround here that works, if you set it as transformable and set its custom class to Date it simply works.

Transformable date

like image 76
Hola Soy Edu Feliz Navidad Avatar answered Sep 19 '22 05:09

Hola Soy Edu Feliz Navidad


Yes, but you might not like it. If you declare the property using a Core Data "Date" type and let Xcode generate the NSManagedObject subclass for you, the property will be an @NSManaged property of type NSDate. As you've figured out, you'd then have to deal with Date vs. NSDate yourself.

If you don't let Xcode generate the subclass for you (in Xcode 8 set "Codegen" to "Manual/None"), you can declare the Core Data "date" property as something like

@NSManaged public var timestamp: Date?

It'll just work. You can read and write Date values, and Core Data will do the right thing. But you become completely responsible for the code in the NSManagedObject subclass. You'll have to create the whole class. If you update the Core Data model, you'll have to update the class as well. Whether this seems worthwhile is up to you but it's the only solution that seems to exist right now.

Update: In Xcode 9, generated code uses Date, so this shouldn't be necessary any more.

like image 40
Tom Harrington Avatar answered Sep 19 '22 05:09

Tom Harrington