diff --git a/GRDB.xcworkspace/contents.xcworkspacedata b/GRDB.xcworkspace/contents.xcworkspacedata index 2ca194e1e..9879bfae9 100644 --- a/GRDB.xcworkspace/contents.xcworkspacedata +++ b/GRDB.xcworkspace/contents.xcworkspacedata @@ -19,9 +19,6 @@ - - diff --git a/Playgrounds/JSONSynchronization.playground/Contents.swift b/Playgrounds/JSONSynchronization.playground/Contents.swift index 479b8acd0..e131cab34 100644 --- a/Playgrounds/JSONSynchronization.playground/Contents.swift +++ b/Playgrounds/JSONSynchronization.playground/Contents.swift @@ -37,9 +37,9 @@ class Person : Record { var id: Int64? var name: String? - func update(from json: NSDictionary) { - id = (json["id"] as? NSNumber)?.int64Value - name = json["name"] as? String + func update(from json: [String: Any]) { + id = (json["id"] as! NSNumber).int64Value + name = (json["name"] as! String) } // Record overrides @@ -67,15 +67,15 @@ class Person : Record { // Synchronizes the persons table with a JSON payload func synchronizePersons(with jsonString: String, in db: Database) throws { let jsonData = jsonString.data(using: .utf8)! - let json = try JSONSerialization.jsonObject(with: jsonData, options: []) as! NSDictionary + let json = try JSONSerialization.jsonObject(with: jsonData, options: []) as! [String: Any] // A support function that extracts an ID from a JSON person. - func jsonPersonId(_ jsonPerson: NSDictionary) -> Int64 { + func jsonPersonId(_ jsonPerson: [String: Any]) -> Int64 { return (jsonPerson["id"] as! NSNumber).int64Value } // Sort JSON persons by id: - let jsonPersons = (json["persons"] as! [NSDictionary]).sorted { + let jsonPersons = (json["persons"] as! [[String: Any]]).sorted { return jsonPersonId($0) < jsonPersonId($1) } diff --git a/Playgrounds/JSONSynchronization.playground/Sources/sortedMerge.swift b/Playgrounds/JSONSynchronization.playground/Sources/sortedMerge.swift index 1e4eb75ad..21edf7bbe 100644 --- a/Playgrounds/JSONSynchronization.playground/Sources/sortedMerge.swift +++ b/Playgrounds/JSONSynchronization.playground/Sources/sortedMerge.swift @@ -42,8 +42,8 @@ public func sortedMerge( left lSeq: LeftSequence, right rSeq: RightSequence, - leftKey: (LeftSequence.Iterator.Element) -> Key, - rightKey: (RightSequence.Iterator.Element) -> Key) -> AnySequence> + leftKey: @escaping (LeftSequence.Iterator.Element) -> Key, + rightKey: @escaping (RightSequence.Iterator.Element) -> Key) -> AnySequence> { return AnySequence { () -> AnyIterator> in var (lGen, rGen) = (lSeq.makeIterator(), rSeq.makeIterator()) diff --git a/Playgrounds/TransactionObserver.playground/Contents.swift b/Playgrounds/TransactionObserver.playground/Contents.swift index 27e63a090..9a151b925 100644 --- a/Playgrounds/TransactionObserver.playground/Contents.swift +++ b/Playgrounds/TransactionObserver.playground/Contents.swift @@ -32,6 +32,10 @@ try! migrator.migrate(dbQueue) class TableChangeObserver : NSObject, TransactionObserver { private var changedTableNames: Set = [] + func observes(eventsOfKind eventKind: DatabaseEventKind) -> Bool { + return true + } + func databaseDidChange(with event: DatabaseEvent) { changedTableNames.insert(event.tableName) } diff --git a/Playgrounds/UserDefaults.playground/Contents.swift b/Playgrounds/UserDefaults.playground/Contents.swift deleted file mode 100644 index a3301a92f..000000000 --- a/Playgrounds/UserDefaults.playground/Contents.swift +++ /dev/null @@ -1,175 +0,0 @@ -// To run this playground, select and build the GRDBOSX scheme. - -import GRDB - -/// UserDefaults is a class that behaves like NSUserDefaults. -/// -/// Usage: -/// -/// let dbQueue = DatabaseQueue(...) -/// dbQueue.inDatabase { db in -/// let defaults = UserDefaults.inDatabase(db) -/// defaults.setInteger(12, forKey: "bar") -/// defaults.integerForKey("bar") // 12 -/// } -public class UserDefaults { - private weak var db: Database! - private var needsTable: Bool = true - private static var registeredDefaults: [UserDefaults] = [] - - /// Returns the defaults for the database - public static func inDatabase(_ db: Database) -> UserDefaults { - registeredDefaults = registeredDefaults.filter { $0.db != nil } - for defaults in registeredDefaults where defaults.db === db { return defaults } - let defaults = UserDefaults(db: db) - registeredDefaults.append(defaults) - return defaults - } - - private init(db: Database) { - self.db = db - } - - private func createTableIfNeeded() { - guard needsTable else { return } - needsTable = false - try! db.execute("CREATE TABLE IF NOT EXISTS \(UserDefaultsItemTableName) (key TEXT NOT NULL PRIMARY KEY, value BLOB)") - } - - - // MARK: - Setting Default Values - - public func removeObject(forKey key: String) { - createTableIfNeeded() - try! db.execute("DELETE FROM \(UserDefaultsItemTableName) WHERE key = ?", arguments: [key]) - } - - public func setBool(_ value: Bool, forKey key: String) { - setObject(NSNumber(value: value), forKey: key) - } - - public func setDouble(_ value: Double, forKey key: String) { - setObject(NSNumber(value: value), forKey: key) - } - - public func setFloat(_ value: Float, forKey key: String) { - setObject(NSNumber(value: value), forKey: key) - } - - public func setInteger(_ value: Int, forKey key: String) { - setObject(NSNumber(value: value), forKey: key) - } - - public func setObject(_ value: AnyObject?, forKey key: String) { - guard let value = value else { - removeObject(forKey: key) - return - } - - createTableIfNeeded() - if var item = UserDefaultsItem.fetchOne(db, key: key) { - item.value = value - try! item.update(db) - } else { - try! UserDefaultsItem(key: key, value: value).insert(db) - } - } - - public func setURL(_ value: NSURL?, forKey key: String) { - setObject(value, forKey: key) - } - - - // MARK: - Reading Default Values - - public var dictionaryRepresentation: [String: AnyObject] { - createTableIfNeeded() - var dic: [String: AnyObject] = [:] - for item in UserDefaultsItem.fetchAll(db) { - dic[item.key] = item.value - } - return dic - } - - public func array(forKey key: String) -> [AnyObject]? { - return object(forKey: key) as? [AnyObject] - } - - public func bool(forKey key: String) -> Bool { - return (object(forKey: key) as? NSNumber)?.boolValue ?? false - } - - public func data(forKey key: String) -> Data? { - return object(forKey: key) as? Data - } - - public func dictionary(forKey key: String) -> [String: AnyObject]? { - return object(forKey: key) as? [String: AnyObject] - } - - public func double(forKey key: String) -> Double { - return (object(forKey: key) as? NSNumber)?.doubleValue ?? 0.0 - } - - public func float(forKey key: String) -> Float { - return (object(forKey: key) as? NSNumber)?.floatValue ?? 0.0 - } - - public func integer(forKey key: String) -> Int { - return (object(forKey: key) as? NSNumber)?.intValue ?? 0 - } - - public func object(forKey key: String) -> AnyObject? { - createTableIfNeeded() - return UserDefaultsItem.fetchOne(db, key: key)?.value - } - - public func stringArray(forKey key: String) -> [String]? { - return object(forKey: key) as? [String] - } - - public func string(forKey key: String) -> String? { - return object(forKey: key) as? String - } - - public func URL(forKey key: String) -> NSURL? { - return object(forKey: key) as? NSURL - } -} - -private struct UserDefaultsItem { - let key: String - var value: AnyObject -} - -extension UserDefaultsItem: RowConvertible { - init(row: Row) { - let data = row.dataNoCopy(named: "value")! - let value = try! PropertyListSerialization.propertyList(from: data, options: [], format: nil) - self.key = row.value(named: "key") - self.value = value - } -} - -extension UserDefaultsItem: Persistable { - static let databaseTableName = UserDefaultsItemTableName - - var persistentDictionary: [String: DatabaseValueConvertible?] { - let data = try! PropertyListSerialization.data(fromPropertyList: value, format: .binary, options: 0) - return ["key": key, "value": data] - } -} - -private let UserDefaultsItemTableName = "grdb_UserDefaults" - - -// ============================================================ - -let dbQueue = DatabaseQueue() -dbQueue.inDatabase { db in - let defaults = UserDefaults.inDatabase(db) - - defaults.integer(forKey: "bar") // 0 - defaults.setInteger(12, forKey: "bar") - defaults.integer(forKey: "bar") // 12 -} diff --git a/Playgrounds/UserDefaults.playground/contents.xcplayground b/Playgrounds/UserDefaults.playground/contents.xcplayground deleted file mode 100644 index 16f9952aa..000000000 --- a/Playgrounds/UserDefaults.playground/contents.xcplayground +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file