mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
Drop RowModel.setDatabaseValue(_:forColumn:).
This leaves RowModel.updateFromRow(_) as the only method that updates a RowModel. RowModel.updateFromRow was introduced in order to let models update compound properties such as CLLocationCoordinate2D. The problem was that RowModel.updateFromRow could be overriden so that it would handle 100% of the updating job, leaving RowModel.setDatabaseValue as a method that could not reliably be used to update a RowModel, and thus had to be dropped.
This commit is contained in:
1 parent
b3e1e9004f
commit
f3a2e5f243
17 files changed
+194
-249
No files matched your search
@@ -21,13 +21,16 @@ class Person : RowModel {
|
||||
return Table(named: "persons", primaryKey: .RowID("id"))
|
||||
}
|
||||
|
||||
override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) {
|
||||
switch column {
|
||||
case "id": id = dbv.value()
|
||||
case "firstName": firstName = dbv.value()
|
||||
case "lastName": lastName = dbv.value()
|
||||
default: super.setDatabaseValue(dbv, forColumn: column)
|
||||
override func updateFromRow(row: Row) {
|
||||
for (column, dbv) in row {
|
||||
switch column {
|
||||
case "id": id = dbv.value()
|
||||
case "firstName": firstName = dbv.value()
|
||||
case "lastName": lastName = dbv.value()
|
||||
default: break
|
||||
}
|
||||
}
|
||||
super.updateFromRow(row) // Subclasses are required to call super.
|
||||
}
|
||||
|
||||
override var storedDatabaseDictionary: [String: DatabaseValueConvertible?] {
|
||||
|
||||
@@ -14,11 +14,14 @@ class MinimalRowID: RowModel {
|
||||
return ["id": id]
|
||||
}
|
||||
|
||||
override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) {
|
||||
switch column {
|
||||
case "id": id = dbv.value()
|
||||
default: super.setDatabaseValue(dbv, forColumn: column)
|
||||
override func updateFromRow(row: Row) {
|
||||
for (column, dbv) in row {
|
||||
switch column {
|
||||
case "id": id = dbv.value()
|
||||
default: break
|
||||
}
|
||||
}
|
||||
super.updateFromRow(row) // Subclasses are required to call super.
|
||||
}
|
||||
|
||||
static func setupInDatabase(db: Database) throws {
|
||||
|
||||
@@ -14,11 +14,14 @@ class MinimalSingle: RowModel {
|
||||
return ["UUID": UUID]
|
||||
}
|
||||
|
||||
override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) {
|
||||
switch column {
|
||||
case "UUID": UUID = dbv.value()
|
||||
default: super.setDatabaseValue(dbv, forColumn: column)
|
||||
override func updateFromRow(row: Row) {
|
||||
for (column, dbv) in row {
|
||||
switch column {
|
||||
case "UUID": UUID = dbv.value()
|
||||
default: break
|
||||
}
|
||||
}
|
||||
super.updateFromRow(row) // Subclasses are required to call super.
|
||||
}
|
||||
|
||||
static func setupInDatabase(db: Database) throws {
|
||||
|
||||
@@ -18,13 +18,16 @@ class Citizenship: RowModel {
|
||||
"native": native]
|
||||
}
|
||||
|
||||
override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) {
|
||||
switch column {
|
||||
case "personName": personName = dbv.value()
|
||||
case "countryName": countryName = dbv.value()
|
||||
case "native": native = dbv.value()
|
||||
default: super.setDatabaseValue(dbv, forColumn: column)
|
||||
override func updateFromRow(row: Row) {
|
||||
for (column, dbv) in row {
|
||||
switch column {
|
||||
case "personName": personName = dbv.value()
|
||||
case "countryName": countryName = dbv.value()
|
||||
case "native": native = dbv.value()
|
||||
default: break
|
||||
}
|
||||
}
|
||||
super.updateFromRow(row) // Subclasses are required to call super.
|
||||
}
|
||||
|
||||
init (personName: String? = nil, countryName: String? = nil, native: Bool? = nil) {
|
||||
|
||||
@@ -13,11 +13,14 @@ class Item: RowModel {
|
||||
return ["name": name]
|
||||
}
|
||||
|
||||
override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) {
|
||||
switch column {
|
||||
case "name": name = dbv.value()
|
||||
default: super.setDatabaseValue(dbv, forColumn: column)
|
||||
override func updateFromRow(row: Row) {
|
||||
for (column, dbv) in row {
|
||||
switch column {
|
||||
case "name": name = dbv.value()
|
||||
default: break
|
||||
}
|
||||
}
|
||||
super.updateFromRow(row) // Subclasses are required to call super.
|
||||
}
|
||||
|
||||
init (name: String? = nil) {
|
||||
|
||||
@@ -21,14 +21,17 @@ class Person: RowModel {
|
||||
]
|
||||
}
|
||||
|
||||
override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) {
|
||||
switch column {
|
||||
case "id": id = dbv.value()
|
||||
case "name": name = dbv.value()
|
||||
case "age": age = dbv.value()
|
||||
case "creationDate": creationDate = (dbv.value() as DatabaseDate?)?.date
|
||||
default: super.setDatabaseValue(dbv, forColumn: column)
|
||||
override func updateFromRow(row: Row) {
|
||||
for (column, dbv) in row {
|
||||
switch column {
|
||||
case "id": id = dbv.value()
|
||||
case "name": name = dbv.value()
|
||||
case "age": age = dbv.value()
|
||||
case "creationDate": creationDate = (dbv.value() as DatabaseDate?)?.date
|
||||
default: break
|
||||
}
|
||||
}
|
||||
super.updateFromRow(row) // Subclasses are required to call super.
|
||||
}
|
||||
|
||||
init (id: Int64? = nil, name: String? = nil, age: Int? = nil, creationDate: NSDate? = nil) {
|
||||
|
||||
@@ -16,12 +16,15 @@ class Pet: RowModel {
|
||||
"name": name]
|
||||
}
|
||||
|
||||
override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) {
|
||||
switch column {
|
||||
case "UUID": UUID = dbv.value()
|
||||
case "name": name = dbv.value()
|
||||
default: super.setDatabaseValue(dbv, forColumn: column)
|
||||
override func updateFromRow(row: Row) {
|
||||
for (column, dbv) in row {
|
||||
switch column {
|
||||
case "UUID": UUID = dbv.value()
|
||||
case "name": name = dbv.value()
|
||||
default: break
|
||||
}
|
||||
}
|
||||
super.updateFromRow(row) // Subclasses are required to call super.
|
||||
}
|
||||
|
||||
init (UUID: String? = nil, name: String? = nil) {
|
||||
|
||||
@@ -13,15 +13,15 @@ class Email : RowModel {
|
||||
return ["email": email, "label": label]
|
||||
}
|
||||
|
||||
override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) {
|
||||
switch column {
|
||||
case "email":
|
||||
self.email = dbv.value()
|
||||
case "label":
|
||||
self.label = dbv.value()
|
||||
default:
|
||||
super.setDatabaseValue(dbv, forColumn: column)
|
||||
override func updateFromRow(row: Row) {
|
||||
for (column, dbv) in row {
|
||||
switch column {
|
||||
case "email": email = dbv.value()
|
||||
case "label": label = dbv.value()
|
||||
default: break
|
||||
}
|
||||
}
|
||||
super.updateFromRow(row) // Subclasses are required to call super.
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,11 +8,14 @@ class IntegerPropertyOnRealAffinityColumn : RowModel {
|
||||
return ["value": value]
|
||||
}
|
||||
|
||||
override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) {
|
||||
switch column {
|
||||
case "value": value = dbv.value()
|
||||
default: super.setDatabaseValue(dbv, forColumn: column)
|
||||
override func updateFromRow(row: Row) {
|
||||
for (column, dbv) in row {
|
||||
switch column {
|
||||
case "value": value = dbv.value()
|
||||
default: break
|
||||
}
|
||||
}
|
||||
super.updateFromRow(row) // Subclasses are required to call super.
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,11 +9,14 @@ class EventRecorder : RowModel {
|
||||
return "eventRecorders"
|
||||
}
|
||||
|
||||
override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) {
|
||||
switch column {
|
||||
case "id": id = dbv.value()
|
||||
default: super.setDatabaseValue(dbv, forColumn: column)
|
||||
override func updateFromRow(row: Row) {
|
||||
for (column, dbv) in row {
|
||||
switch column {
|
||||
case "id": id = dbv.value()
|
||||
default: break
|
||||
}
|
||||
}
|
||||
super.updateFromRow(row) // Subclasses are required to call super.
|
||||
}
|
||||
|
||||
override var storedDatabaseDictionary: [String: DatabaseValueConvertible?] {
|
||||
|
||||
@@ -10,11 +10,14 @@ class PersonWithOverrides: Person {
|
||||
var extra: Int!
|
||||
var lastSavingMethod: SavingMethod?
|
||||
|
||||
override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) {
|
||||
switch column {
|
||||
case "extra": extra = dbv.value()
|
||||
default: super.setDatabaseValue(dbv, forColumn: column)
|
||||
override func updateFromRow(row: Row) {
|
||||
for (column, dbv) in row {
|
||||
switch column {
|
||||
case "extra": extra = dbv.value()
|
||||
default: break
|
||||
}
|
||||
}
|
||||
super.updateFromRow(row) // Subclasses are required to call super.
|
||||
}
|
||||
|
||||
override func insert(db: Database) throws {
|
||||
|
||||
@@ -54,16 +54,17 @@ class Placemark : RowModel {
|
||||
}
|
||||
}
|
||||
|
||||
// Update other columns
|
||||
super.updateFromRow(row)
|
||||
}
|
||||
|
||||
override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) {
|
||||
switch column {
|
||||
case "id": id = dbv.value()
|
||||
case "name": name = dbv.value()
|
||||
default: super.setDatabaseValue(dbv, forColumn: column)
|
||||
// Other columns
|
||||
for (column, dbv) in row {
|
||||
switch column {
|
||||
case "id": id = dbv.value()
|
||||
case "name": name = dbv.value()
|
||||
default: break
|
||||
}
|
||||
}
|
||||
|
||||
// Subclasses are required to call super.
|
||||
super.updateFromRow(row)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,11 +32,14 @@ Usage:
|
||||
return ["birthDate": DatabaseDate(birthDate), ...]
|
||||
}
|
||||
|
||||
override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) {
|
||||
switch column {
|
||||
case "birthDate": birthDate = (dbv.value() as DatabaseDate?)?.date
|
||||
case ...
|
||||
default: super.setDatabaseValue(dbv, forColumn: column)
|
||||
override func updateFromRow(row: Row) {
|
||||
for (column, dbv) in row {
|
||||
switch column {
|
||||
case "birthDate": birthDate = (dbv.value() as DatabaseDate?)?.date
|
||||
case ...
|
||||
}
|
||||
}
|
||||
super.updateFromRow(row) // Subclasses are required to call super.
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+37
-69
@@ -7,7 +7,7 @@ designed to be subclassed.
|
||||
Subclasses opt in RowModel features by overriding all or part of the core
|
||||
methods that define their relationship with the database:
|
||||
|
||||
- setDatabaseValue(_:forColumn:)
|
||||
- updateFromRow(_)
|
||||
- databaseTable
|
||||
- storedDatabaseDictionary
|
||||
*/
|
||||
@@ -23,44 +23,6 @@ public class RowModel {
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Core methods
|
||||
|
||||
/**
|
||||
Returns a table definition.
|
||||
|
||||
The insert, update, save, delete and reload methods require it: they raise
|
||||
a fatal error if databaseTableName is nil.
|
||||
|
||||
The implementation of the base class RowModel returns nil.
|
||||
*/
|
||||
public class var databaseTableName: String? {
|
||||
return nil
|
||||
}
|
||||
|
||||
/**
|
||||
Returns the values that should be stored in the database.
|
||||
|
||||
Subclasses must include primary key columns, if any, in the returned
|
||||
dictionary.
|
||||
|
||||
The implementation of the base class RowModel returns an empty dictionary.
|
||||
*/
|
||||
public var storedDatabaseDictionary: [String: DatabaseValueConvertible?] {
|
||||
return [:]
|
||||
}
|
||||
|
||||
/**
|
||||
Updates `self` with a database value.
|
||||
|
||||
The implementation of the base class RowModel does nothing.
|
||||
|
||||
- parameter dbv: A DatabaseValue.
|
||||
- parameter column: A column name.
|
||||
*/
|
||||
public func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) {
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Initializers
|
||||
|
||||
/**
|
||||
@@ -104,6 +66,41 @@ public class RowModel {
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Core methods
|
||||
|
||||
/**
|
||||
Returns a table definition.
|
||||
|
||||
The insert, update, save, delete and reload methods require it: they raise
|
||||
a fatal error if databaseTableName is nil.
|
||||
|
||||
The implementation of the base class RowModel returns nil.
|
||||
*/
|
||||
public class var databaseTableName: String? {
|
||||
return nil
|
||||
}
|
||||
|
||||
/**
|
||||
Returns the values that should be stored in the database.
|
||||
|
||||
Subclasses must include primary key columns, if any, in the returned
|
||||
dictionary.
|
||||
|
||||
The implementation of the base class RowModel returns an empty dictionary.
|
||||
*/
|
||||
public var storedDatabaseDictionary: [String: DatabaseValueConvertible?] {
|
||||
return [:]
|
||||
}
|
||||
|
||||
/**
|
||||
Updates self from a row.
|
||||
|
||||
*Important*: subclasses must invoke super's implementation.
|
||||
*/
|
||||
public func updateFromRow(row: Row) {
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Events
|
||||
|
||||
/**
|
||||
@@ -115,18 +112,7 @@ public class RowModel {
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Update
|
||||
|
||||
/**
|
||||
Updates self from a row.
|
||||
|
||||
*Important*: subclasses must invoke super's implementation.
|
||||
*/
|
||||
public func updateFromRow(row: Row) {
|
||||
for (column, databaseValue) in row {
|
||||
setDatabaseValue(databaseValue, forColumn: column)
|
||||
}
|
||||
}
|
||||
// MARK: - Copy
|
||||
|
||||
/**
|
||||
Updates `self` from `other.storedDatabaseDictionary`.
|
||||
@@ -223,24 +209,6 @@ public class RowModel {
|
||||
fatalError("\(self.dynamicType).storedDatabaseDictionary must return the value for the primary key `(managedColumn)`")
|
||||
}
|
||||
if rowID == nil {
|
||||
// IMPLEMENTATION NOTE:
|
||||
//
|
||||
// We update the ID with updateFromRow(), and not
|
||||
// setDatabaseValue(_:forColumn:). Rationale:
|
||||
//
|
||||
// 1. If subclass updates its ID in setDatabaseValue(), then the
|
||||
// default updateFromRow() runs, which calls
|
||||
// setDatabaseValue(), and updates the ID.
|
||||
//
|
||||
// 2. If subclass overrides updateFromRow() and updates its ID
|
||||
// in setDatabaseValue(), then the subclasses calls super
|
||||
// from updateFromRow() (as it is required to do), which in
|
||||
// turns call setDatabaseValue(), and updates the ID.
|
||||
//
|
||||
// 3. If subclass overrides updateFromRow() and updates its ID
|
||||
// in updateFromRow(), not in setDatabaseValue(), which it is
|
||||
// allowed to do, then using setDatabaseValue() would not
|
||||
// update the ID.
|
||||
updateFromRow(Row(dictionary: [managedColumn: changes.insertedRowID]))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,13 +39,16 @@ class Person : RowModel {
|
||||
return "persons"
|
||||
}
|
||||
|
||||
override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) {
|
||||
switch column {
|
||||
case "id": id = dbv.value()
|
||||
case "firstName": firstName = dbv.value()
|
||||
case "lastName": lastName = dbv.value()
|
||||
default: super.setDatabaseValue(dbv, forColumn: column)
|
||||
override func updateFromRow(row: Row) {
|
||||
for (column, dbv) in row {
|
||||
switch column {
|
||||
case "id": id = dbv.value()
|
||||
case "firstName": firstName = dbv.value()
|
||||
case "lastName": lastName = dbv.value()
|
||||
default: break
|
||||
}
|
||||
}
|
||||
super.updateFromRow(row) // Subclasses are required to call super.
|
||||
}
|
||||
|
||||
override var storedDatabaseDictionary: [String: DatabaseValueConvertible?] {
|
||||
|
||||
@@ -125,7 +125,6 @@ To fiddle with the library, open the `GRDB.xcworkspace` workspace: it contains a
|
||||
- [RowModel API Quick Tour](#rowmodel-api-quick-tour)
|
||||
- [Core Methods](#core-methods)
|
||||
- [Fetching Row Models](#fetching-row-models)
|
||||
- [Compound Properties](#compound-properties)
|
||||
- [Ad Hoc Subclasses](#ad-hoc-subclasses)
|
||||
- [Tables and Primary Keys](#tables-and-primary-keys)
|
||||
- [Insert, Update and Delete](#insert-update-and-delete)
|
||||
@@ -400,22 +399,22 @@ DatabaseDate.fetchAll(db, "SELECT ...") // [DatabaseDate?]
|
||||
DatabaseDate.fetchOne(db, "SELECT ...") // DatabaseDate?
|
||||
```
|
||||
|
||||
Use NSDate in a [Row Model](#row-models):
|
||||
Use NSDate in a RowModel (see [Fetching Row Models](#fetching-row-models) for more information):
|
||||
|
||||
```swift
|
||||
class Person : RowModel {
|
||||
var birthDate: NSDate?
|
||||
|
||||
|
||||
override var storedDatabaseDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["birthDate": DatabaseDate(birthDate), ...]
|
||||
}
|
||||
|
||||
override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) {
|
||||
switch column {
|
||||
case "birthDate": birthDate = (dbv.value() as DatabaseDate?)?.date
|
||||
case ...
|
||||
default: super.setDatabaseValue(dbv, forColumn: column)
|
||||
|
||||
override func updateFromRow(row: Row) {
|
||||
if let dbv = row["birthDate"] {
|
||||
let dbDate = dbv.value() as DatabaseDate?
|
||||
birthDate = dbDate?.date
|
||||
}
|
||||
...
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -455,12 +454,12 @@ DatabaseDateComponents.fetchAll(db, "SELECT ...") // [DatabaseDateComponents?]
|
||||
DatabaseDateComponents.fetchOne(db, "SELECT ...") // DatabaseDateComponents?
|
||||
```
|
||||
|
||||
Use NSDateComponents in a [Row Model](#row-models):
|
||||
Use NSDate in a RowModel (see [Fetching Row Models](#fetching-row-models) for more information):
|
||||
|
||||
```swift
|
||||
class Person : RowModel {
|
||||
var birthDateComponents: NSDateComponents?
|
||||
|
||||
|
||||
override var storedDatabaseDictionary: [String: DatabaseValueConvertible?] {
|
||||
// Store birth date as YYYY-MM-DD:
|
||||
let dbComponents = DatabaseDateComponents(
|
||||
@@ -468,15 +467,13 @@ class Person : RowModel {
|
||||
format: .YMD)
|
||||
return ["birthDate": dbComponents, ...]
|
||||
}
|
||||
|
||||
override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) {
|
||||
switch column {
|
||||
case "birthDate":
|
||||
let dbComponents: DatabaseDateComponents? = dbv.value()
|
||||
|
||||
override func updateFromRow(row: Row) {
|
||||
if let dbv = row["birthDate"] {
|
||||
let dbComponents = dbv.value() as DatabaseDateComponents?
|
||||
birthDateComponents = dbComponents?.dateComponents
|
||||
case ...
|
||||
default: super.setDatabaseValue(dbv, forColumn: column)
|
||||
}
|
||||
...
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -821,7 +818,6 @@ Yet, it does a few things well:
|
||||
- [RowModel API Quick Tour](#rowmodel-api-quick-tour)
|
||||
- [Core Methods](#core-methods)
|
||||
- [Fetching Row Models](#fetching-row-models)
|
||||
- [Compound Properties](#compound-properties)
|
||||
- [Ad Hoc Subclasses](#ad-hoc-subclasses)
|
||||
- [Tables and Primary Keys](#tables-and-primary-keys)
|
||||
- [Insert, Update and Delete](#insert-update-and-delete)
|
||||
@@ -874,11 +870,11 @@ Person.fetchOne(statement, arguments: ...) // Person?
|
||||
|
||||
Subclasses opt in RowModel features by overriding all or part of the core methods that define their relationship with the database:
|
||||
|
||||
| Core Methods | fetch | insert | update | delete | reload |
|
||||
|:-------------------------------- |:-----:|:------:|:------:|:------:|:------:|
|
||||
| `setDatabaseValue(_:forColumn:)` | ✓ | | | | ✓ |
|
||||
| `databaseTableName` | | ✓ | ✓ | ✓ | ✓ |
|
||||
| `storedDatabaseDictionary` | | ✓ | ✓ | ✓ | ✓ |
|
||||
| Core Methods | fetch | insert | update | delete | reload |
|
||||
|:-------------------------- |:-----:|:------:|:------:|:------:|:------:|
|
||||
| `updateFromRow` | ✓ | | | | ✓ |
|
||||
| `databaseTableName` | | ✓ | ✓ | ✓ | ✓ |
|
||||
| `storedDatabaseDictionary` | | ✓ | ✓ | ✓ | ✓ |
|
||||
|
||||
|
||||
### Fetching Row Models
|
||||
@@ -893,17 +889,21 @@ class Person : RowModel {
|
||||
}
|
||||
```
|
||||
|
||||
The `setDatabaseValue(_:forColumn:)` method assigns database values to properties:
|
||||
The `updateFromRow` method assigns database values to properties:
|
||||
|
||||
```swift
|
||||
class Person : RowModel {
|
||||
override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) {
|
||||
switch column {
|
||||
case "id": id = dbv.value()
|
||||
case "age": age = dbv.value()
|
||||
case "name": name = dbv.value()
|
||||
default: super.setDatabaseValue(dbv, forColumn: column)
|
||||
override func updateFromRow(row: Row) {
|
||||
// Iterate (ColumnName, DatabaseValue) pairs in row:
|
||||
for (column, dbv) in row {
|
||||
switch column {
|
||||
case "id": id = dbv.value()
|
||||
case "age": age = dbv.value()
|
||||
case "name": name = dbv.value()
|
||||
default: break
|
||||
}
|
||||
}
|
||||
super.updateFromRow(row) // Subclasses are required to call super.
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -939,70 +939,6 @@ for person in persons { ... } // OK
|
||||
```
|
||||
|
||||
|
||||
#### Compound Properties
|
||||
|
||||
Some properties don't fit well in a single column:
|
||||
|
||||
```swift
|
||||
class Placemark : RowModel {
|
||||
// Stored in two columns: latitude and longitude
|
||||
var coordinate: CLLocationCoordinate2D?
|
||||
}
|
||||
```
|
||||
|
||||
A solution is of course to declare two convenience properties, latitude and longitude, and implement coordinate on top of them, as a computed property. `setDatabaseValue()` would then update latitude and longitude separately:
|
||||
|
||||
```swift
|
||||
// Solution 1: computed property:
|
||||
class Placemark : RowModel {
|
||||
var latitude: CLLocationDegrees?
|
||||
var longitude: CLLocationDegrees?
|
||||
var coordinate: CLLocationCoordinate2D? {
|
||||
switch (latitude, longitude) {
|
||||
case (let latitude?, let longitude?):
|
||||
// Both latitude and longitude are not nil.
|
||||
return CLLocationCoordinate2DMake(latitude, longitude)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) {
|
||||
switch column {
|
||||
case "latitude": latitude = dbv.value()
|
||||
case "longitude": longitude = dbv.value()
|
||||
default: super.setDatabaseValue(dbv, forColumn: column)
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Another solution is to override the `updateFromRow()` method, where you can process a row as a whole:
|
||||
|
||||
```swift
|
||||
// Solution 2: process rows as a whole:
|
||||
class Placemark : RowModel {
|
||||
var coordinate: CLLocationCoordinate2D?
|
||||
|
||||
override func updateFromRow(row: Row) {
|
||||
if let latitude = row["latitude"], let longitude = row["longitude"] {
|
||||
// Both columns are present.
|
||||
switch (latitude.value() as Double?, longitude.value() as Double?) {
|
||||
case (let latitude?, let longitude?):
|
||||
// Both latitude and longitude are not nil.
|
||||
coordinate = CLLocationCoordinate2DMake(latitude, longitude)
|
||||
default:
|
||||
coordinate = nil
|
||||
}
|
||||
}
|
||||
super.updateFromRow(row) // Subclasses are required to call super.
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The remaining columns are handled by `setDatabaseValue()` as described above.
|
||||
|
||||
|
||||
#### Ad Hoc Subclasses
|
||||
|
||||
Swift makes it very easy to create small and private types. This is a wonderful opportunity to create **ad hoc subclasses** that provide support for custom queries with extra columns.
|
||||
@@ -1016,11 +952,11 @@ class PersonsViewController: UITableViewController {
|
||||
private class PersonViewModel : Person {
|
||||
var bookCount: Int!
|
||||
|
||||
override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) {
|
||||
switch column {
|
||||
case "bookCount": bookCount = dbv.value()
|
||||
default: super.setDatabaseValue(dbv, forColumn: column)
|
||||
override func updateFromRow(row: Row) {
|
||||
if dbv = row["bookCount"] {
|
||||
bookCount = dbv.value()
|
||||
}
|
||||
super.updateFromRow(row) // Subclasses are required to call super.
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1172,12 +1108,15 @@ class Person : RowModel {
|
||||
}
|
||||
|
||||
/// Updates `self` with a database value.
|
||||
override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) {
|
||||
switch column {
|
||||
case "id": id = dbv.value()
|
||||
case ...
|
||||
default: super.setDatabaseValue(dbv, forColumn: column)
|
||||
override func updateFromRow(row: Row) {
|
||||
// Iterate (ColumnName, DatabaseValue) pairs in row:
|
||||
for (column, dbv) in row {
|
||||
switch column {
|
||||
case "id": id = dbv.value()
|
||||
case...
|
||||
}
|
||||
}
|
||||
super.updateFromRow(row) // Subclasses are required to call super.
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ Release Notes
|
||||
|
||||
- `RowModel.edited` has been renamed `RowModel.databaseEdited`.
|
||||
- `RowModel.databaseTable` has been replaced with `RowModel.databaseTableName` which returns a String.
|
||||
- `RowModel.setDatabaseValue(_:forColumn:)` has been removed. Use and override `RowModel.updateFromRow(_)` instead.
|
||||
|
||||
|
||||
## 0.11.0
|
||||
|
||||
Reference in new issue
Block a user