-
-
Notifications
You must be signed in to change notification settings - Fork 731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DateComponents best practises? #764
Comments
Hello and thank you, @mtancock Let's start from your app:
It's indeed frequent to only care about YMD. Not all date-related contexts such as calendar (gregorian, japanese, locale-dependent, etc.) or time zones (local, utc, etc.) are always relevant. For example, YMD is a perfect storage format for birth dates (although it may be tactful to make the year optional). Your first question should be about the type of those dates, the type that you want your application to deal with. It's too early to think about the database. Just think about the domain you are modelling, and the constraint you want your type to express.
Make up your mind, and now we can dig onto the database level. Whatever your choice of a type is, we have a "problem" to solve: there's no direct and built-in database support for We have a situation where the database does not talk the same language as the rest of the application. Well, this is not exceptional at all, and a record type is the perfect place to solve this divergence. A "classic" example of such situation is the following: the database speaks "latitude" and "longitude", but the rest of the application speaks "coordinate": struct Place: Codable, FetchableRecord, PersistableRecord {
var id: String
private var latitude: CLLocationDegrees
private var longitude: CLLocationDegrees
var coordinate: CLLocationCoordinate2D {
get {
CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
}
set {
latitude = newValue.latitude
longitude = newValue.longitude
}
}
} How does it apply to struct Activity: Codable, FetchableRecord, PersistableRecord {
var id: String
var name: String
private var date: DatabaseDateComponents
var dateComponents: DateComponents {
get {
date.dateComponents
}
set {
date = DatabaseDateComponents(newValue, format: .YMD)
}
}
} The strategy is always the same:
In some very rare occasions, you may want to extend a type with database support. This is discussed in the Custom Value Types. But I'd strongly recommend against - the strategy described above is much more simple and clear. |
;-) |
Ah thanks @groue . That's an amazing answer - and you're right. I was too focussed on thinking about the database format. Quick follow up question: It seems like |
Oops, a missing piece! What do you prefer: submitting a pull request, or wait? 😉 |
I'll take a look, but no promises! 😀 |
No problem, of course. You've found a missing piece, so a fix will ship. I just can't provide any ETA, and it may have to wait until GRDB 5. This can be solved today, in your app, by adding Codable conformance yourself. You'll be very close from a pull request, then. |
What did you do?
Good question...
I'm storing information which includes dates, however the dates don't need to be precise - I'm only interested in YMD. I read in the readme that GRDB supports reading/writing to the db as dateComponents, but having a hard time visualising how I should set this up.
I'm currently creating my records as structs e.g
My migration looks like this:
I've tried using date directly, and at one point I had three separate fields for YMD as Ints, and then recreated them after fetching from the database but that seems annoying. Then I read the Readme (should have started there...) and see that there's support for
DateComponents
viaDatabaseDateComponents
but in terms of defining my record, saving back to the db etc. , I'm not sure how I should be using it. Any tips or suggestions where I should be looking to figure this out?Thanks for all your work on this library. It's great.
Environment
GRDB flavor(s): Regular GRDB
GRDB version: 4.13
Installation method: Cocoapods
Xcode version: 11.3.1
Swift version: 5ish
Platform(s) running GRDB: iOS/macOS
macOS version running Xcode: 10.15.4
The text was updated successfully, but these errors were encountered: