mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-29 04:24:21 +00:00
DESIGN.md
This commit is contained in:
1 parent
45c1bc7fd1
commit
395634ef6b
1 file changed
+7
-6
@@ -3,9 +3,11 @@ The Design of GRDB.swift
|
||||
|
||||
More than caveats or defects, there are a few glitches, or surprises in the GRDB.swift API. We try to explain them here.
|
||||
|
||||
- **Why can't NSDate, NSData, UIImage adopt DatabaseValueConvertible, so that they can be used as query arguments, or fetched from the database?**
|
||||
- **Why can't NSDate, NSData and other classes adopt DatabaseValueConvertible, so that they can be used as query arguments, or fetched from the database?**
|
||||
|
||||
This is because the `DatabaseValueConvertible` protocol requires adopting types to implement an initializer:
|
||||
Indeed, those types are indirectly supported: GRDB's Blob handles NSData, DatabaseDate handles NSDate, etc.
|
||||
|
||||
This is because the `DatabaseValueConvertible` protocol requires adopting types to implement an initializer. Initializers can't be added to an extension of an existing non-final class, and all our fellow Objective-C friends are left out of the game.
|
||||
|
||||
```swift
|
||||
public protocol DatabaseValueConvertible {
|
||||
@@ -17,12 +19,12 @@ More than caveats or defects, there are a few glitches, or surprises in the GRDB
|
||||
}
|
||||
```
|
||||
|
||||
This `init?(databaseValue:)` initializer can't be added to an extension of an existing non-final class, and thus prevents our fellow Objective-C friends to adopt it.
|
||||
|
||||
That's it. And I don't expect this constraint to be lifted in a future version of the language. In Swift, subclasses don't have to support the non-required initializers of their superclass, and thus one can not add a required initializer in a class extension. In other words, the *potentiality* of an NSDate subclass that does not support DatabaseValueConvertible's initializer prevents NSDate to adopt it.
|
||||
Don't expect this constraint to be lifted in a future version of the language. In Swift, subclasses don't have to support the non-required initializers of their superclass, and thus one can not add a required initializer in a class extension. In other words, the *potentiality* of an NSDate subclass that does not support DatabaseValueConvertible's initializer prevents NSDate to adopt it.
|
||||
|
||||
This is an inconvenience for GRDB, because we need to define wrappers in order to support non-final classes. GRDB indeed ships with Blob, DatabaseDate and DatabaseDateComponents that wrap NSData, NSDate, and NSDateComponents.
|
||||
|
||||
**Rationale**
|
||||
|
||||
Yet we gain a big advantage: *DatabaseValueConvertible is not generic*. Being not generic, we can talk about arrays and dictionaries of heterogenous values adopting the protocol: `[DatabaseValueConvertible?]` and `[String: DatabaseValueConvertible?]` are usable types. And this is why you can easily provide query arguments:
|
||||
|
||||
```swift
|
||||
@@ -38,7 +40,6 @@ More than caveats or defects, there are a few glitches, or surprises in the GRDB
|
||||
- Provide and document built-in types that support the ultra-common NSData, NSDate and NSDateComponents.
|
||||
- Extra bonus: there are several ways to store dates in an SQLite database. The built-in DatabaseDate type provides a reasonable option, but it may not fit all needs. Fortunately, NSDate has not been polluted, and the user can write her own date support.
|
||||
|
||||
|
||||
**Exploration**
|
||||
|
||||
Were there any other options?
|
||||
|
||||
Reference in new issue
Block a user