Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 {
private var changedTableNames: Set<String> = []
func databaseDidChangeWithEvent(event: DatabaseEvent) {
// Remember the name of the changed table:
changedTableNames.insert(event.tableName)
}
func databaseWillCommit() throws {
// Let go
}
func databaseDidCommit(db: Database) {
// Extract the names of changed tables, and reset until next
// database event:
let changedTableNames = self.changedTableNames
self.changedTableNames = []
// Notify
dispatch_async(dispatch_get_main_queue()) {
NSNotificationCenter.defaultCenter().postNotificationName(
DatabaseTablesDidChangeNotification,
object: self,
userInfo: [ChangedTableNamesKey: changedTableNames])
}
}
func databaseDidRollback(db: Database) {
// Reset until next database event:
changedTableNames = []
}
}
// Register observer
let dbQueue = DatabaseQueue()
let observer = TableChangeObserver()
dbQueue.addTransactionObserver(observer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.