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 perlasivakrishna/706a577bdd55fe45ae8176d7e4c0d143 to your computer and use it in GitHub Desktop.
Save perlasivakrishna/706a577bdd55fe45ae8176d7e4c0d143 to your computer and use it in GitHub Desktop.
CoreData + ManagedObject + Codable + Tranformable Entity
import UIKit
import CoreData
@objc(RouteResponse)
public class RouteResponse: NSManagedObject, Codable {
@NSManaged var routeInfo: [Route]!
@NSManaged var routeTimings: [String: [RouteTime]]!
enum CodingKeys: CodingKey {
case routeInfo, routeTimings
}
required convenience public init(from decoder: Decoder) throws {
guard let contextUserInfoKey = CodingUserInfoKey(rawValue: "context"),
let managedObjectContext = decoder.userInfo[contextUserInfoKey] as? NSManagedObjectContext,
let entity = NSEntityDescription.entity(forEntityName: "RouteResponse", in: managedObjectContext) else {
fatalError()
}
self.init(entity: entity, insertInto: managedObjectContext)
let values = try decoder.container(keyedBy: CodingKeys.self)
do {
routeInfo = try values.decode([Route].self, forKey: .routeInfo)
routeTimings = try values.decode([String: [RouteTime]].self, forKey: .routeTimings)
} catch {
print(error)
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(routeInfo, forKey: .routeInfo)
try container.encode(routeTimings, forKey: .routeTimings)
}
}
@objc(Route)
public class Route: NSManagedObject, Codable {
@NSManaged var id: String!
@NSManaged var name: String!
@NSManaged var source: String!
@NSManaged var tripDuration: String!
@NSManaged var destination: String!
enum CodingKeys: CodingKey {
case id, name, source, tripDuration, destination
}
required convenience public init(from decoder: Decoder) throws {
guard let contextUserInfoKey = CodingUserInfoKey(rawValue: "context"),
let managedObjectContext = decoder.userInfo[contextUserInfoKey] as? NSManagedObjectContext,
let entity = NSEntityDescription.entity(forEntityName: "Route", in: managedObjectContext) else {
fatalError("Cannot decode Route!")
}
self.init(entity: entity, insertInto: managedObjectContext)
do {
let values = try decoder.container(keyedBy: CodingKeys.self)
id = try values.decode(String.self, forKey: .id)
name = try values.decode(String.self, forKey: .name)
source = try values.decode(String.self, forKey: .source)
destination = try values.decode(String.self, forKey: .destination)
tripDuration = try values.decode(String.self, forKey: .tripDuration)
} catch {
print(error)
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(id, forKey: .id)
try container.encode(name, forKey: .name)
try container.encode(source, forKey: .source)
try container.encode(destination, forKey: .destination)
try container.encode(tripDuration, forKey: .tripDuration)
}
}
@objc(RouteTime)
public class RouteTime: NSManagedObject, Codable {
enum CodingKeys: CodingKey {
case totalSeats, avaiable, tripStartTime
}
required convenience public init(from decoder: Decoder) throws {
guard let contextUserInfoKey = CodingUserInfoKey(rawValue: "context"),
let managedObjectContext = decoder.userInfo[contextUserInfoKey] as? NSManagedObjectContext,
let entity = NSEntityDescription.entity(forEntityName: "RouteTime", in: managedObjectContext) else {
fatalError("Cannot decode RouteTime!")
}
self.init(entity: entity, insertInto: managedObjectContext)
let values = try decoder.container(keyedBy: CodingKeys.self)
totalSeats = try values.decode(Int.self, forKey: .totalSeats)
avaiable = try values.decode(Int.self, forKey: .avaiable)
tripStartTime = try values.decode(String.self, forKey: .tripStartTime)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(totalSeats, forKey: .totalSeats)
try container.encode(avaiable, forKey: .avaiable)
try container.encode(tripStartTime, forKey: .tripStartTime)
}
//
public var totalSeats: Int? {
get {
willAccessValue(forKey: "totalSeats")
defer { didAccessValue(forKey: "totalSeats") }
return primitiveValue(forKey: "totalSeats") as? Int
}
set {
willChangeValue(forKey: "totalSeats")
defer { didChangeValue(forKey: "totalSeats") }
guard let value = newValue else {
setPrimitiveValue(nil, forKey: "totalSeats")
return
}
setPrimitiveValue(value, forKey: "totalSeats")
}
}
public var avaiable: Int? {
get {
willAccessValue(forKey: "avaiable")
defer { didAccessValue(forKey: "avaiable") }
return primitiveValue(forKey: "avaiable") as? Int
}
set {
willChangeValue(forKey: "avaiable")
defer { didChangeValue(forKey: "avaiable") }
guard let value = newValue else {
setPrimitiveValue(nil, forKey: "avaiable")
return
}
setPrimitiveValue(value, forKey: "avaiable")
}
}
@NSManaged var tripStartTime: String!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment