Documentation for compound RowModel properties.

This commit is contained in:
Gwendal Roué committed 2015-09-03 13:57:10 +02:00
1 parent 5fd80035e0
commit 5d71555049
2 files changed
+71 -1

No files matched your search

+71
View File
@@ -129,6 +129,7 @@ To fiddle with the library, open the `GRDB.xcworkspace` workspace: it contains a
- [Row Models](#row-models)
- [Fetching Row Models](#fetching-row-models)
- [Ad Hoc Subclasses](#ad-hoc-subclasses)
- [Compound Properties](#compound-properties)
- [Tables and Primary Keys](#tables-and-primary-keys)
- [Insert, Update and Delete](#insert-update-and-delete)
- [Preventing Useless UPDATE Statements](#preventing-useless-update-statements)
@@ -875,6 +876,76 @@ class PersonsViewController: UITableViewController {
```
### 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 internal properties, latitude and longitude, and implement coordinate as a computed property. `setDatabaseValue()` would simply update latitude and longitude separately:
```swift
// Solution 1: computed property:
class Placemark : RowModel {
var coordinate: CLLocationCoordinate2D? {
switch (latitude, longitude) {
case (let latitude?, let longitude?):
// Both latitude and longitude are not nil.
return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
default:
return nil
}
}
var latitude: CLLocationDegrees?
var longitude: CLLocationDegrees?
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) {
// Let's keep things simple, and only update self.coordinate if the
// row contains both lat and long columns.
//
// We test column presence by extracting DatabaseValues, which may
// contain coordinates, or NULL:
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 = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
default:
coordinate = nil
}
}
// Subclasses are required to call super:
super.updateFromRow(row)
}
}
```
The remaining columns are handled by `setDatabaseValue()` as described above.
### Tables and Primary Keys
Declare a **Table** given its **name** and **primary key** in order to fetch row models by ID:
-1
View File
@@ -1,4 +1,3 @@
- [ ] #2: this commit may be how stephencelis fixed it: https://github.com/stephencelis/SQLite.swift/commit/8f64e357c3a6668c5f011c91ba33be3e8d4b88d0
- [ ] Use @warn_unused_result
- [ ] How can RowModel wrap two columns in a single CLLocationCoordinate2D property?
- [ ] Now that the user can create its own rows, RowModel.init(row) is no longer a way to distinguish fetched models. So let's remove init(row), or turn it into a convenience initializer. UPDATE: we still need a required initializer. If it is not RowModel.init(row), then it must be RowModel.init(). Unfortunately, this prevents subclasses to provide initializers only made of optional arguments, such as Person.init(name: String? = nil) (rdar://22554816). So we can't stop requiring RowModel.init(row). Instead, we have to document that this initializer is *not* a way to surely distinguish fetched models from models created in another fashion.