Skip to content

Instantly share code, notes, and snippets.

View groue's full-sized avatar

Gwendal Roué groue

View GitHub Profile
@groue
groue / DatabaseTimestamp.swift
Last active January 16, 2016 16:21
A type that reads and stores NSDate as timestamp in an SQLite database http://github.com/groue/GRDB.swift
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
@groue
groue / TableChangeObservers.swift
Last active May 12, 2018 07:43
Notification of table changes in an SQLite database http://github.com/groue/GRDB.swift
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 {
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
setupDatabase()
return true
}
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)
}
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 " +
")")
import GRDB
class Person: Record {
var id: Int64?
var name: String
var score: Int
}
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
let person = Person(name: "Arthur", score: 100)
try person.insert(dbQueue)
let persons = Person.fetchAll(dbQueue) // [Persons]
import UIKit
import GRDB
class PersonsViewController: UITableViewController {
var personsController: FetchedRecordsController<Person>!
override func viewDidLoad() {
super.viewDidLoad()
// The persons, sorted by score then by name
// 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