New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
EVReflection with NSManagedObject CRASHES! #190
Comments
Hmm... This seems like an issue. You should create your objects as you used to do when using a NSManagedObject like using the init with the parameters entity and insertInto. After that you however are able to set the properties from the json by using this:
But maybe you could also create a designated initialiser on your managedObject class. That would be something like:
|
I will have a look if I could create a NSManagedObject extension or subclass that will solve this issue. But that will take some time. Please try my suggestion from the comment above. I will keep this issue open so that it keeps visible as a todo item |
@evermeer Thanks a LOT for your Fast reply. Please keep me updated if you modify this to improve NSmanagedObject handling and also Keep up the great Work :-) 👍 |
If you do have a designated initialiser working and willing to share it, then please let me know. |
Hi @evermeer: We came up with a Designated Initialiser that is Actually Working:
I Hope this helps someone :-) |
Nice,
I will see If I can change this into a NSManagedObject extension and add it
as a CoreData subspec to EVReflection.
I do have a question. You are making sure that only properties from
entity?.properties will be set. Is that really needed? If there is a json
property which is not in entity.properties, then you should see a warning
which could be ignored or suppressed by using the propertyMapping function.
What if you have properties in your object which you do want to populate
from json but not want to write to coredata?
I was thinking of making the code like this (untested!)
public extension NSManagedObject {
public convenience required init(entityName: String =
EVReflection.swiftStringFromClass(self),
insertIntoManagedObjectContext
context: NSManagedObjectContext!,
json: String?,
forKeyPath: String? = nil) {
let entity = NSEntityDescription.entity(forEntityName: entityName,
in: context)
self.init(entity: entity!, insertInto: context)
let jsonDictionary = EVReflection.dictionaryFromJson(json)
EVReflection.setPropertiesfromDictionary(jsonDictionary, anyObject:
self, forKeyPath: forKeyPath)
}
}
2017-05-03 17:53 GMT+02:00 hernanarber <notifications@github.com>:
… Hi @evermeer <https://github.com/evermeer>:
We came up with a Designated Initialiser that is Actually Working:
`public class NGTripSummary: NSManagedObject {
// Each NSManagedObject that use EVReflection need to use this init
// Only copy and change the forEntityName to your className
public convenience required init(json: String?,
forKeyPath: String? = nil, insertIntoManagedObjectContext context:
NSManagedObjectContext!) {
let entity = NSEntityDescription.entity(forEntityName: "NGTripSummary", in: context)
self.init(entity: entity!, insertInto: context)
let jsonDictionary = EVReflection.dictionaryFromJson(json)
let propertiesDictionay:NSMutableDictionary = [:]
for propertyInObject in (entity?.properties)! {
if jsonDictionary.allKeys.contains(where: { $0 as! String == propertyInObject.name }) {
propertiesDictionay.setValue(jsonDictionary[propertyInObject.name], forKey: propertyInObject.name)
}
}
EVReflection.setPropertiesfromDictionary(propertiesDictionay, anyObject: self, forKeyPath: forKeyPath)
}
}`
I Hope this helps someone :-)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#190 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAH_EuJ5RZISp3CzVtOvt_aQxAMbf58Eks5r2KLtgaJpZM4NGR5E>
.
|
Hi @evermeer: Yes your code is much more generic, and it Would be nice to have it as an extension for NSManagedObject+EVReflection However, in our experience, the Properties that were in the JSON and NOT in the Object were not only producing a warning but actually CRASHING... Anyway, It's great to see that you are keeping this wonderful Library Alive and Updated ;-) Cheers |
Ah, true, In order to have full functionality and flexibility all functions from EVObject should be implemented. It's best to create a base class for that and use that instead of NSManagedObject. That's what I will do in the EVReflection subspec. you will then get something like this:
|
I now have created a CoreData subscpec. It's not yet published to Cocoapods. I first want to create an array initialiser. Here some information about the subspec: The test can be found here: |
The array initialiser is now also ready. I am now publishing this as version 4.10.0. In half an hour it's published to Cocoapods. |
AWESOME @evermeer! Hey come work with us @ Engie 👍 www.engieapp.com |
Tel Aviv is a bit far for a daily commute. |
BTW: is there a possibility that the GitHub is Updated but the POD is not yet there? We Tried to Pod Update It and it doesn't include the CoreData Folder :-s |
It should be there you podfile should contain: |
it looks like sub objects needs to be set using the propertyMapping function. I will add a unit test to show how. I don't see any other easier way to initiate objects with the right context automatically. I will close this issue and create an improvement issue for that. |
hI: I've been using EVReflection to make our Network Layer Fully Restful and I Must say: AWESOME WORK! Thanks for this Library.
Now, to the Issue:
The Next Step is to get Those Object Straight into CORE DATA. Here is one of the Classes in Question
// Here is the Object With the EVReflectable Extension as the Documentation Claims:
// and HERE is the Object Properties:
// NGTripSummary+CoreDataProperties.swift
// . here is the JSON String that Represents a Demo Object:
// and HERE is the Method I'm Trying to use to Create this Mock Object:
// NOW: When Creating the Object, the Crash Happens HERE @ class EVReflection:
////////////////////////////////////////// REASON FOR CRASH /////////////////////////////////////
2017-04-24 17:51:47.195791+0300 engieApp[6570:2198102] [error] error: CoreData: error: Failed to call designated initializer on NSManagedObject class 'engieApp.NGTripSummary'
CoreData: error: CoreData: error: Failed to call designated initializer on NSManagedObject class 'engieApp.NGTripSummary'
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
@evermeer: Please help us! you are the last hope of our Galaxy (dev-team), may the Force be with you :-) 👍
The text was updated successfully, but these errors were encountered: