mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-29 04:24:21 +00:00
Update playgrounds
This commit is contained in:
1 parent
6e6701a644
commit
a82cc8cbd4
6 files changed
+12
-190
No files matched your search
-3
@@ -19,9 +19,6 @@
|
||||
<FileRef
|
||||
location = "group:TransactionObserver.playground">
|
||||
</FileRef>
|
||||
<FileRef
|
||||
location = "group:UserDefaults.playground">
|
||||
</FileRef>
|
||||
<FileRef
|
||||
location = "group:MyPlayground.playground">
|
||||
</FileRef>
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -42,8 +42,8 @@
|
||||
public func sortedMerge<LeftSequence: Sequence, RightSequence: Sequence, Key: Comparable>(
|
||||
left lSeq: LeftSequence,
|
||||
right rSeq: RightSequence,
|
||||
leftKey: (LeftSequence.Iterator.Element) -> Key,
|
||||
rightKey: (RightSequence.Iterator.Element) -> Key) -> AnySequence<MergeStep<LeftSequence.Iterator.Element, RightSequence.Iterator.Element>>
|
||||
leftKey: @escaping (LeftSequence.Iterator.Element) -> Key,
|
||||
rightKey: @escaping (RightSequence.Iterator.Element) -> Key) -> AnySequence<MergeStep<LeftSequence.Iterator.Element, RightSequence.Iterator.Element>>
|
||||
{
|
||||
return AnySequence { () -> AnyIterator<MergeStep<LeftSequence.Iterator.Element, RightSequence.Iterator.Element>> in
|
||||
var (lGen, rGen) = (lSeq.makeIterator(), rSeq.makeIterator())
|
||||
|
||||
@@ -32,6 +32,10 @@ try! migrator.migrate(dbQueue)
|
||||
class TableChangeObserver : NSObject, TransactionObserver {
|
||||
private var changedTableNames: Set<String> = []
|
||||
|
||||
func observes(eventsOfKind eventKind: DatabaseEventKind) -> Bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func databaseDidChange(with event: DatabaseEvent) {
|
||||
changedTableNames.insert(event.tableName)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<playground version='5.0' target-platform='osx' last-migration='0800'>
|
||||
<timeline fileName='timeline.xctimeline'/>
|
||||
</playground>
|
||||
Reference in new issue
Block a user