This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import GRDB | |
struct DatabaseTimestamp: DatabaseValueConvertible { | |
// NSDate conversion | |
// | |
// Value types should consistently use the Swift nil to represent the | |
// database NULL: the date property is a non-optional NSDate. | |
let date: NSDate | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import GRDB | |
/// The notification posted when database tables have changed: | |
let DatabaseTablesDidChangeNotification = "DatabaseTablesDidChangeNotification" | |
let ChangedTableNamesKey = "ChangedTableNames" | |
/// TableChangeObserver posts a DatabaseTablesDidChangeNotification on the main | |
/// thread after database tables have changed. | |
class TableChangeObserver : NSObject, TransactionObserverType { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
var window: UIWindow? | |
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { | |
setupDatabase() | |
return true | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import GRDB | |
/// The shared database queue. | |
var dbQueue: DatabaseQueue! | |
func setupDatabase() { | |
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first! as NSString | |
let databasePath = documentsPath.stringByAppendingPathComponent("db.sqlite") | |
dbQueue = try! DatabaseQueue(path: databasePath) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func setupDatabase() { | |
// That "collation" helps us compare person names in a localized case insensitive fashion | |
let collation = DatabaseCollation.localizedCaseInsensitiveCompare | |
try! dbQueue.execute( | |
"CREATE TABLE persons (" + | |
"id INTEGER PRIMARY KEY, " + | |
"name TEXT NOT NULL COLLATE \(collation.name), " + | |
"score INTEGER NOT NULL " + | |
")") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import GRDB | |
class Person: Record { | |
var id: Int64? | |
var name: String | |
var score: Int | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import GRDB | |
class Person: Record { | |
var id: Int64? | |
var name: String | |
var score: Int | |
init(name: String, score: Int) { | |
self.name = name | |
self.score = score |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let person = Person(name: "Arthur", score: 100) | |
try person.insert(dbQueue) | |
let persons = Person.fetchAll(dbQueue) // [Persons] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import GRDB | |
class PersonsViewController: UITableViewController { | |
var personsController: FetchedRecordsController<Person>! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// The persons, sorted by score then by name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK: - UITableViewDataSource | |
extension FetchedRecordsControllerDemoViewController { | |
override func numberOfSectionsInTableView(tableView: UITableView) -> Int { | |
return personsController.sections.count | |
} | |
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return personsController.sections[section].numberOfRecords |