Skip to content

Commit

Permalink
Linuxification pass Oyvindkg#2
Browse files Browse the repository at this point in the history
  • Loading branch information
Krug committed Aug 23, 2016
1 parent cac8b14 commit a3cf7c9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Sources/Filter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftyDB+Asynchronous.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
31 changes: 28 additions & 3 deletions Sources/SwiftyDB.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -406,8 +427,7 @@ extension SwiftyDB {
*/

private func objectWithData <D where D: Storable, D: NSObject> (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
Expand All @@ -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
}
Expand Down

0 comments on commit a3cf7c9

Please sign in to comment.