Create NSManagedObject from AnyClass
Hi @JohnEstropia ,
First of all thanks for your great library.
I have a question regarding the newest version. In Swift 3.x versions I was able to create a new NSManagedObject from AnyClass. This does not seem to work anymore with the newest libraries.
Here is a code extract I used which is not working anymore:
tx.create(Into(myCoreDataClass))
Can you please give me a hint on how to create an Object from an arbitrary class in the new version.
Thanks a lot
@mrichtsfeld Into and From clauses are now generic types. See https://github.com/JohnEstropia/CoreStore/issues/257#issuecomment-397517052
Thanks for the quick reply @JohnEstropia
I have changed the code now and it compiles.
tx.create(Into<AirplaneBase>(airplaneInfo.coreDataClass))
The airplaneInfo.coreDataClass is a subclass of AirplaneBase
Still it seems as if the proper object is not created. I feed the created class into an object monitor and get a crash at this line of code:
fetchRequest.entity = context.parentStack!.entityDescription(for: EntityIdentifier(self.entityClass))!
because the entitiyClass is NSManagedObject and not the expected subclass of AirplaneBase.
Do I misunderstand this?
Thanks a lot for any more help on this issue.
@mrichtsfeld What type is returned from coreDataClass?
Normally, you can query using tx.create(Into<AirplaneBase>()), which means that you are fetching an AirplaneBase type and you are casting the result to AirplaneBase.
If you have a case where you want to fetch a type but cast it into another, you can write tx.create(Into<superclass type>(fetch type)). Usually you only want to do this if you are casting to a base class.
If you are not getting the correct type during fetch, then either your airplaneInfo.coreDataClass is returning the wrong class, or your entity is not in the model.
@JohnEstropia the returned type is of type AT01which is a subtype for AirplaneBase.
But in this method fetchRequest.entity = context.parentStack!.entityDescription(for: EntityIdentifier(self.entityClass))!the entityClassis NSManagedObjectand not AirplaneBase. So the method entityDescriptiondoes not find anything which causes the following crash (Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value)
The initiator is a call to tx.monitorObject(airplane) where airplaneis of type AirplaneBase.
Do I need to cast anything else or explicitly define a type?
Thanks again for your help.