Search code examples
iosswiftcore-datareflectionnsmanagedobject

EVReflection with NSManagedObject Crashes / not Working


hI: I've been using EVReflection to make our Network Layer Fully Restful and I Must say: AWESOME WORK! Thanks to @evermeer for this Library. you can get it here: https://github.com/evermeer/EVReflection

Now, to the Issue:

The Next Step is to get those Objects Straight into CORE DATA. Here is one of the Classes in Question

// Here is the Object With the EVReflectable Extension as the Documentation Claims:

import Foundation
import CoreData
import EVReflection

public class NGTripSummary: NSManagedObject { }

extension NGTripSummary: EVReflectable { }

// and HERE are the Object Properties:

// NGTripSummary+CoreDataProperties.swift

import Foundation
import CoreData

extension NGTripSummary {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<NGTripSummary> {
        return NSFetchRequest<NGTripSummary>(entityName: "NGTripSummary")
    }

    @NSManaged public var carId: Int64
    @NSManaged public var citiesVisited: NSObject?
    @NSManaged public var cost: Double
    @NSManaged public var distance: Double
    @NSManaged public var globalStartDate: NSDate?
    @NSManaged public var globalEndDate: NSDate?
    @NSManaged public var kpl: Double
    @NSManaged public var litres: Double
    @NSManaged public var routeLocations: NSObject?
    @NSManaged public var sessionId: Int64
    @NSManaged public var localStartDate: NSDate?
    @NSManaged public var localEndDate: NSDate?
    @NSManaged public var duration: Int64
    @NSManaged public var speed: Double
    @NSManaged public var _id: Int64
    @NSManaged public var sessionUuid: String?
    @NSManaged public var tripUuid: String?

}

// . here is the JSON String that Represents a Demo Object:

let tripData = "{\"id\":26105240,\"userId\":25796277,\"carId\":25817551,\"vehicleId\":57812351,\"sessionUuid\":\"53324259-aa69-41c8-8f9e-c62bdb70f165\",\"tripUuid\":\"afdd8f55-6d14-4cf9-bd9f-5b6da47aaf93\",\"localStartDate\":1487170622490,\"localEndDate\":1487178323654,\"globalStartDate\":1487163422490,\"globalEndDate\":1487171123654,\"routeLocations\":null,\"litres\":24.7699,\"kpl\":0.0772,\"cost\":153.3258,\"distance\":1.9132,\"duration\":491.958,\"speed\":14.0}"

// and HERE is the Method I'm Trying to use to Create this Mock Object:

func makeMockData() {
    let singleTrip = NGTripSummary(json: tripData)
    print("Single Trip: \(singleTrip)")
} 

// NOW: When Creating the Object, the Crash Happens HERE @ class EVReflection:

        // Call your own object validators that comply to the format: validate<Key>:Error:
            do {
                var setValue: AnyObject? = value as AnyObject?
/* LINE 923: CRASH HAPPENS HERE -> */  try anyObject.validateValue(&setValue, forKey: key)
                anyObject.setValue(setValue, forKey: key)
            } catch _ {
                (anyObject as? EVReflectable)?.addStatusMessage(.InvalidValue, message: "Not a valid value for object `\(NSStringFromClass(type(of: (anyObject as AnyObject))))`, type `\(type)`, key  `\(key)`, value `\(value)`")
                print("INFO: Not a valid value for object `\(NSStringFromClass(type(of: (anyObject as AnyObject))))`, type `\(type)`, key  `\(key)`, value `\(value)`")
            }

////////////////// REASON FOR CRASH ////////////////////

[error] error: CoreData: error: Failed to call designated initializer on NSManagedObject class 'NGTripSummary' CoreData: error: CoreData: error: Failed to call designated initializer on NSManagedObject class 'NGTripSummary'

//////////////////////////////////////////////////

-> Anybody, PLEASE HELP :-0


Solution

  • EVReflection now has a Cocoapods subspec for CoreData. For more information see https://github.com/evermeer/EVReflection/tree/master/Source/CoreData

    It will let you write code like this:\

    let moc: NSManagedObjectContext = EVReflectionTestsData().moc // Your code for getting the NSManagedObjectContext.
    
    let obj = CoreDataPerson(context: moc, json: "{\"firstName\" : \"Edwin\", \"lastName\" : \"Vermeer\"}")
    
    try! moc.save() //TODO: implement error handling