diff --git a/GRDB.xcworkspace/contents.xcworkspacedata b/GRDB.xcworkspace/contents.xcworkspacedata index fc0fea041..83fcba978 100644 --- a/GRDB.xcworkspace/contents.xcworkspacedata +++ b/GRDB.xcworkspace/contents.xcworkspacedata @@ -10,6 +10,9 @@ + + diff --git a/Playgrounds/TransactionObserver.playground/Contents.swift b/Playgrounds/TransactionObserver.playground/Contents.swift new file mode 100644 index 000000000..ffa0dd640 --- /dev/null +++ b/Playgrounds/TransactionObserver.playground/Contents.swift @@ -0,0 +1,78 @@ +//: Playground - noun: a place where people can play + +import GRDB + + +// Create the databsae + +var configuration = Configuration() +let dbQueue = DatabaseQueue(configuration: configuration) // Memory database +var migrator = DatabaseMigrator() +migrator.registerMigration("createPersons") { db in + try db.execute("CREATE TABLE persons (" + + "id INTEGER PRIMARY KEY, " + + "name TEXT NOT NULL " + + ")") +} +migrator.registerMigration("createPets") { db in + try db.execute("CREATE TABLE pets (" + + "id INTEGER PRIMARY KEY, " + + "name TEXT, " + + "ownerId INTEGER NOT NULL REFERENCES persons(id) ON DELETE CASCADE" + + ")") +} +try! migrator.migrate(dbQueue) + + + +/// TableChangeObserver prints on the main thread the changes database tables. +class TableChangeObserver : NSObject, TransactionObserverType { + private var changedTableNames: Set = [] + + func databaseDidChangeWithEvent(event: DatabaseEvent) { + changedTableNames.insert(event.tableName) + } + + func databaseWillCommit() throws { + } + + func databaseDidCommit(db: Database) { + print("Changed table(s): \(changedTableNames.joinWithSeparator(", "))") + changedTableNames = [] + } + + func databaseDidRollback(db: Database) { + changedTableNames = [] + } +} + + + +// Register observer + +dbQueue.inDatabase { db in + db.addTransactionObserver(TableChangeObserver()) +} + + +// + +print("-- Changes 1") +try! dbQueue.inDatabase { db in + let arthurId = try db.execute("INSERT INTO persons (name) VALUES (?)", arguments: ["Arthur"]).insertedRowID + let barbaraId = try db.execute("INSERT INTO persons (name) VALUES (?)", arguments: ["Barbara"]).insertedRowID + let pet = try db.execute("INSERT INTO pets (ownerId, name) VALUES (?, ?)", arguments: [arthurId, "Barbara"]).insertedRowID +} + +print("-- Changes 2") +try dbQueue.inTransaction { db in + try db.execute("INSERT INTO persons (name) VALUES ('Arthur')").insertedRowID + try db.execute("INSERT INTO persons (name) VALUES ('Barbara')").insertedRowID + return .Rollback +} + + +print("-- Changes 3") +try dbQueue.inDatabase { db in + try db.execute("DELETE FROM persons") +} diff --git a/Playgrounds/TransactionObserver.playground/contents.xcplayground b/Playgrounds/TransactionObserver.playground/contents.xcplayground new file mode 100644 index 000000000..06828af92 --- /dev/null +++ b/Playgrounds/TransactionObserver.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Playgrounds/TransactionObserver.playground/timeline.xctimeline b/Playgrounds/TransactionObserver.playground/timeline.xctimeline new file mode 100644 index 000000000..bf468afec --- /dev/null +++ b/Playgrounds/TransactionObserver.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + +