Skip to content

Instantly share code, notes, and snippets.

@NeedMoreDesu
Created November 15, 2017 13:42
Show Gist options
  • Save NeedMoreDesu/f600abdd23c8074b9de1e9a5bde62cc3 to your computer and use it in GitHub Desktop.
Save NeedMoreDesu/f600abdd23c8074b9de1e9a5bde62cc3 to your computer and use it in GitHub Desktop.
//
// Car+Extensions.swift
// SomeExampleApp
//
// Created by Oleksii Horishnii on 11/14/17.
// Copyright © 2017 Oleksii Horishnii. All rights reserved.
//
import Foundation
import CoreData
import RxDataSources
import RxCoreData
extension Car : Equatable {
static func == (lhs: Car, rhs: Car) -> Bool {
return lhs.identity == rhs.identity
}
}
extension Car : Persistable {
typealias T = NSManagedObject
static var entityName: String {
return "Car"
}
static var primaryAttributeName: String {
return "id"
}
var identity: String {
return "\(self.date.hashValue)"
}
init(entity: T) {
self.date = entity.value(forKey: "date") as! Date
self.id = entity.value(forKey: "id") as! String
self.model = entity.value(forKey: "model") as! String
self.price = entity.value(forKey: "price") as! Decimal
self.engine = entity.value(forKey: "engine") as! String
self.transmission = entity.value(forKey: "transmission") as! String
self.condition = entity.value(forKey: "condition") as! String
}
func update(_ entity: T) {
entity.setValue(date!, forKey: "date")
entity.setValue(id!, forKey: "id")
entity.setValue(model!, forKey: "model")
entity.setValue(price!, forKey: "price")
entity.setValue(engine!, forKey: "engine")
entity.setValue(transmission!, forKey: "transmission")
entity.setValue(condition!, forKey: "condition")
do {
try entity.managedObjectContext?.save()
} catch let e {
print(e)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment