Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcinczenko/8c9bf9289d9f3a2eade4946b1e67af6e to your computer and use it in GitHub Desktop.
Save marcinczenko/8c9bf9289d9f3a2eade4946b1e67af6e to your computer and use it in GitHub Desktop.
The playground for the blog post "Distributed persistence with Swift and CoreDatat" at http://blog.redgreenrefactor.eu/post/146625336664/distributed-persistence-with-swift-and-coredata
import Foundation
import CoreData
protocol EPQuantity {
associatedtype EPQuantityValueType
func getValue() -> EPQuantityValueType
}
struct EPAcceleration: EPQuantity, CustomStringConvertible {
let acceleration: Double
var description: String {
return "EPAcceleration(acceleration: \(acceleration))"
}
func getValue() -> Double {
return acceleration
}
}
struct EPSpeed: EPQuantity, CustomStringConvertible {
let speed: Int
var description: String {
return "EPSpeed(speed: \(speed))"
}
func getValue() -> Int {
return speed
}
}
protocol EPPersistable {}
extension Int: EPPersistable {}
extension Double: EPPersistable {}
protocol EPCoreDataQuantity {
associatedtype EPCoreDataQuantityValueType
func getValue() -> EPCoreDataQuantityValueType
func setValue(value: EPCoreDataQuantityValueType)
}
extension EPSpeedCoreData:EPCoreDataQuantity {
func getValue() -> Int {
return Int(speed)
}
func setValue(value: Int) {
speed = Int32(value)
}
}
@objc(EPSpeedCoreData)
class EPSpeedCoreData: NSManagedObject {
// Insert code here to add functionality to your managed object subclass
}
extension EPSpeedCoreData {
@NSManaged var speed: Int32
}
protocol EPPersistance {
associatedtype EPQuantityType
func write(item: EPQuantityType)
func read() -> [EPQuantityType]?
}
class EPCoreDataWrapper<T: EPCoreDataQuantity, U: EPQuantity where T.EPCoreDataQuantityValueType: EPPersistable, U.EPQuantityValueType: EPPersistable>: EPPersistance {
func write(item: U) {
print("Writing item: \(item)")
}
func read() -> [U]? {
print("Reading items.")
return nil
}
}
class EPAnyPersistance<T: EPQuantity where T.EPQuantityValueType: EPPersistable> : EPPersistance {
let _write: T -> ()
let _read: () -> [T]?
init<Base: EPPersistance where Base.EPQuantityType == T>(base: Base) {
_write = base.write
_read = base.read
}
func write(item: T) {
_write(item)
}
func read() -> [T]? {
return _read()
}
}
class EPDataStore<T: EPQuantity where T.EPQuantityValueType : EPPersistable> {
let persistance: EPAnyPersistance<T>
init(persistance: EPAnyPersistance<T>) {
self.persistance = persistance
}
func store(item: T) {
persistance.write(item)
}
func getAll() -> [T]? {
return persistance.read()
}
}
let dataStoreForSpeed = EPDataStore<EPSpeed>(persistance: EPAnyPersistance(base: EPCoreDataWrapper<EPSpeedCoreData, EPSpeed>()))
dataStoreForSpeed.store(EPSpeed(speed: 125))
dataStoreForSpeed.getAll()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment