From a3cf7c967ee703ad4bbf207f03b8b88f5cdd7208 Mon Sep 17 00:00:00 2001 From: Krug Date: Tue, 23 Aug 2016 17:49:39 +0200 Subject: [PATCH] Linuxification pass #2 --- Sources/Filter.swift | 4 ++++ Sources/SwiftyDB+Asynchronous.swift | 4 ++-- Sources/SwiftyDB.swift | 31 ++++++++++++++++++++++++++--- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/Sources/Filter.swift b/Sources/Filter.swift index 15bc443..eb743d3 100644 --- a/Sources/Filter.swift +++ b/Sources/Filter.swift @@ -51,7 +51,11 @@ public class Filter: ExpressibleByDictionaryLiteral { let relationship: Relationship let value: Any? + #if os(Linux) + private let uniqueifier: UInt32 = UInt32(random()) + #else private let uniqueifier: UInt32 = arc4random() + #endif var uniquePropertyName: String { return "\(propertyName)\(uniqueifier)" diff --git a/Sources/SwiftyDB+Asynchronous.swift b/Sources/SwiftyDB+Asynchronous.swift index 529aea8..b17a787 100644 --- a/Sources/SwiftyDB+Asynchronous.swift +++ b/Sources/SwiftyDB+Asynchronous.swift @@ -15,7 +15,7 @@ extension SwiftyDB { /** A global, concurrent queue with default priority */ #if os(Linux) private var queue: dispatch_queue_t { - return dispatch_get_global_queue(.QOS_CLASS_USER_INITIATED) + return dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) } #else private var queue: DispatchQueue { @@ -79,7 +79,7 @@ extension SwiftyDB { return } - completionHandler?(self!.dataForType(type: type, matchingFilter: filter)) + completionHandler(self!.dataForType(type: type, matchingFilter: filter)) }) #else queue.async() { [weak self] () -> Void in diff --git a/Sources/SwiftyDB.swift b/Sources/SwiftyDB.swift index 46b0ec2..76fd669 100644 --- a/Sources/SwiftyDB.swift +++ b/Sources/SwiftyDB.swift @@ -14,6 +14,10 @@ import tinysqlite public protocol Storable { /** Used to initialize an object to get information about its properties */ init() + #if os(Linux) + /* because of the lack of reflection and KVO-related methods, we need to explicitly map variables back */ + init(storable: [String: AnyObject]) + #endif } /** Implement this protocol to use primary keys */ @@ -357,10 +361,27 @@ public class SwiftyDB { case is Bool.Type: return row.boolForColumn(name: propertyData.name!) as? Value case is NSArray.Type: + #if os(Linux) + // temporarily, PREVIEW-6 has the right method + if let data = row.sdataForColumn(name: propertyData.name!) { + return NSKeyedUnarchiver.unarchiveObjectWithData(data) as? NSArray + } else { + return nil + } + #else return NSKeyedUnarchiver.unarchiveObject(with: row.dataForColumn(name: propertyData.name!)! as Data) as? NSArray + #endif case is NSDictionary.Type: + #if os(Linux) + // temporarily, PREVIEW-6 has the right method + if let data = row.sdataForColumn(name: propertyData.name!) { + return NSKeyedUnarchiver.unarchiveObjectWithData(data) as? NSDictionary + } else { + return nil + } + #else return NSKeyedUnarchiver.unarchiveObject(with: row.dataForColumn(name: propertyData.name!)! as Data) as? NSDictionary - + #endif default: return nil } @@ -406,8 +427,7 @@ extension SwiftyDB { */ private func objectWithData (data: [String: Value?], forType type: D.Type) -> D { - let object = (type as NSObject.Type).init() as! D - + var validData: [String: AnyObject] = [:] data.forEach { (name, value) -> () in @@ -416,7 +436,12 @@ extension SwiftyDB { } } + #if os(Linux) + let object = D.init(storable: validData) + #else + let object = (type as NSObject.Type).init() as! D object.setValuesForKeys(validData) + #endif return object }